do not place projs late
[libfirm] / ir / ir / iredges.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/iredges.c
4  * Purpose:     Always available outs.
5  * Author:      Sebastian Hack
6  * Modified by: Michael Beck
7  * Created:     14.1.2005
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2006 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14  * Always available outs.
15  * @author Sebastian Hack
16  * @date 14.1.2005
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #ifdef HAVE_ALLOCA_H
24 #include <alloca.h>
25 #endif
26 #ifdef HAVE_MALLOC_H
27 #include <malloc.h>
28 #endif
29
30 #include "irnode_t.h"
31 #include "iropt_t.h"
32 #include "iredgekinds.h"
33 #include "iredges_t.h"
34 #include "irgwalk.h"
35 #include "irdump_t.h"
36 #include "irprintf.h"
37 #include "irhooks.h"
38 #include "debug.h"
39 #include "set.h"
40
41 /**
42 * A function that allows for setting an edge.
43 * This abstraction is necessary since different edge kind have
44 * different methods of setting edges.
45 */
46 typedef void (set_edge_func_t)(ir_node *src, int pos, ir_node *tgt);
47
48 typedef int (get_edge_src_arity_func_t)(const ir_node *src);
49
50 typedef int (get_edge_src_first_func_t)(const ir_node *src);
51
52 typedef ir_node *(get_edge_src_n_func_t)(const ir_node *src, int pos);
53
54 /**
55 * Additional data for an edge kind.
56 */
57 typedef struct {
58         const char                *name;
59         set_edge_func_t           *set_edge;
60         get_edge_src_first_func_t *get_first;
61         get_edge_src_arity_func_t *get_arity;
62         get_edge_src_n_func_t     *get_n;
63 } ir_edge_kind_info_t;
64
65 static int get_zero(const ir_node *irn)
66 {
67         return 0;
68 }
69
70 static int get_irn_first(const ir_node *irn)
71 {
72         return 0 - !is_Block(irn);
73 }
74
75 static ir_node *get_block_n(const ir_node *irn, int pos)
76 {
77         return is_Block(irn) ? get_Block_cfgpred_block((ir_node *) irn, pos) : 0;
78 }
79
80 static const ir_edge_kind_info_t edge_kind_info[EDGE_KIND_LAST] = {
81         { "normal"     , set_irn_n,   get_irn_first, get_irn_arity,  get_irn_n   },
82         { "block succs", NULL,        get_zero,      get_irn_arity,  get_block_n },
83         { "dependency",  set_irn_dep, get_zero,      get_irn_deps,   get_irn_dep }
84 };
85
86 #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)
87 #define get_n(irn, pos, kind)        (edge_kind_info[kind].get_n(irn, pos))
88 #define get_kind_str(kind)           (edge_kind_info[kind].name)
89
90 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
91
92 /**
93  * This flag is set to 1, if the edges get initialized for an irg.
94  * Then register additional data is forbidden.
95  */
96 static int edges_used = 0;
97
98 static int edges_private_size = 0;
99
100 int edges_register_private_data(size_t n)
101 {
102         int res = edges_private_size;
103
104         assert(!edges_used && "you cannot register private edge data, if edges have been initialized");
105
106         edges_private_size += n;
107         return res;
108 }
109
110 #define TIMES37(x) (((x) << 5) + ((x) << 2) + (x))
111
112 #define get_irn_out_list_head(irn) (&get_irn_out_info(irn)->outs)
113
114 static int edge_cmp(const void *p1, const void *p2, size_t len)
115 {
116         const ir_edge_t *e1 = p1;
117         const ir_edge_t *e2 = p2;
118         int res = e1->src == e2->src && e1->pos == e2->pos;
119
120         return !res;
121 }
122
123 #define edge_hash(edge) (TIMES37((edge)->pos) + HASH_PTR((edge)->src))
124
125 /**
126  * Initialize the out information for a graph.
127  * @note Dead node elimination can call this on an already initialized graph.
128  */
129 void edges_init_graph_kind(ir_graph *irg, ir_edge_kind_t kind)
130 {
131         if(edges_activated_kind(irg, kind)) {
132                 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
133                 int amount = 2048;
134
135                 edges_used = 1;
136                 if(info->edges) {
137                         amount = set_count(info->edges);
138                         del_set(info->edges);
139                 }
140                 info->edges = new_set(edge_cmp, amount);
141         }
142 }
143
144 /**
145  * Get the edge object of an outgoing edge at a node.
146  * @param   irg The graph, the node is in.
147  * @param   src The node at which the edge originates.
148  * @param   pos The position of the edge.
149  * @return      The corresponding edge object or NULL,
150  *              if no such edge exists.
151  */
152 const ir_edge_t *get_irn_edge_kind(ir_graph *irg, const ir_node *src, int pos, ir_edge_kind_t kind)
153 {
154         if(edges_activated_kind(irg, kind)) {
155                 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
156                 ir_edge_t key;
157
158                 key.src = (ir_node *) src;
159                 key.pos = pos;
160                 return set_find(info->edges, &key, sizeof(key), edge_hash(&key));
161         }
162
163         return NULL;
164 }
165
166 /**
167  * Change the out count
168  */
169 static INLINE void edge_change_cnt(ir_node *tgt, ir_edge_kind_t kind, int ofs) {
170         irn_edge_info_t *info = _get_irn_edge_info(tgt, kind);
171         info->out_count += ofs;
172 }
173
174 /* The edge from (src, pos) -> old_tgt is redirected to tgt */
175 void edges_notify_edge_kind(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt, ir_edge_kind_t kind, ir_graph *irg)
176 {
177         const char *msg = "";
178
179         if(!edges_activated_kind(irg, kind))
180                 return;
181
182         /*
183          * Only do something, if the old and new target differ.
184          */
185         if(tgt != old_tgt) {
186                 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
187                 set *edges            = info->edges;
188                 ir_edge_t *templ      = alloca(sizeof(templ[0]));
189                 ir_edge_t *edge;
190
191                 /* Initialize the edge template to search in the set. */
192                 memset(templ, 0, sizeof(templ[0]));
193                 templ->src     = src;
194                 templ->pos     = pos;
195                 templ->invalid = 0;
196                 templ->present = 0;
197                 templ->kind    = kind;
198                 DEBUG_ONLY(templ->src_nr = get_irn_node_nr(src));
199
200                 /*
201                  * If the target is NULL, the edge shall be deleted.
202                  */
203                 if (tgt == NULL) {
204                         /* search the edge in the set. */
205                         edge = set_find(edges, templ, sizeof(templ[0]), edge_hash(templ));
206
207                         /* mark the edge invalid if it was found */
208                         if(edge) {
209                                 msg = "deleting";
210                                 list_del(&edge->list);
211                                 edge->invalid = 1;
212                                 edge->pos = -2;
213                                 edge->src = NULL;
214                         }
215
216                         /* If the edge was not found issue a warning on the debug stream */
217                         else {
218                                 msg = "edge to delete not found!\n";
219                         }
220                 } /* if */
221
222                 /*
223                  * The target is not NULL and the old target differs
224                  * from the new target, the edge shall be moved (if the
225                  * old target was != NULL) or added (if the old target was
226                  * NULL).
227                  */
228                 else {
229                         struct list_head *head = _get_irn_outs_head(tgt, kind);
230
231                         assert(head->next && head->prev &&
232                                         "target list head must have been initialized");
233
234                         /*
235                          * insert the edge, if it is not yet in the set or return
236                          * the instance in the set.
237                          */
238                         edge = set_insert(edges, templ, sizeof(templ[0]), edge_hash(templ));
239
240 #ifdef DEBUG_libfirm
241                         assert(!edge->invalid && "Invalid edge encountered");
242 #endif
243
244                         /* If the old target is not null, the edge is moved. */
245                         if(old_tgt) {
246                                 msg = "redirecting";
247
248                                 list_move(&edge->list, head);
249                                 edge_change_cnt(old_tgt, kind, -1);
250                         }
251
252                         /* The old target was null, thus, the edge is newly created. */
253                         else {
254                                 msg = "adding";
255                                 list_add(&edge->list, head);
256                         }
257
258                         edge_change_cnt(tgt, kind, +1);
259                 } /* else */
260         }
261
262         /* If the target and the old target are equal, nothing is done. */
263         DBG((dbg, LEVEL_5, "announce out edge: %+F %d-> %+F(%+F): %s\n", src, pos, tgt, old_tgt, msg));
264 }
265
266 void edges_notify_edge(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt, ir_graph *irg)
267 {
268         edges_notify_edge_kind(src, pos, tgt, old_tgt, EDGE_KIND_NORMAL, irg);
269         if(is_Block(src)) {
270                 /* do not use get_nodes_block() here, it fails when running unpinned */
271                 ir_node *bl_old = old_tgt ? get_irn_n(skip_Proj(old_tgt), -1) : NULL;
272                 ir_node *bl_tgt = NULL;
273
274                 if(tgt)
275                         bl_tgt = is_Bad(tgt) ? tgt : get_irn_n(skip_Proj(tgt), -1);
276
277                 edges_notify_edge_kind(src, pos, bl_tgt, bl_old, EDGE_KIND_BLOCK, irg);
278         }
279 }
280
281
282 void edges_node_deleted_kind(ir_node *old, ir_edge_kind_t kind, ir_graph *irg)
283 {
284         if(edges_activated_kind(irg, kind)) {
285                 int i, n;
286
287                 DBG((dbg, LEVEL_5, "node deleted (kind: %s): %+F\n", get_kind_str(kind), old));
288
289                 foreach_tgt(old, i, n, kind) {
290                         ir_node *old_tgt = get_n(old, i, kind);
291                         edges_notify_edge_kind(old, i, NULL, old_tgt, kind, irg);
292                 }
293         }
294 }
295
296 struct build_walker {
297         ir_graph *irg;
298         ir_edge_kind_t kind;
299 };
300
301 /**
302  * Post-Walker: notify all edges
303  */
304 static void build_edges_walker(ir_node *irn, void *data) {
305         struct build_walker *w = data;
306         int i, n;
307
308         foreach_tgt(irn, i, n, w->kind)
309                 edges_notify_edge_kind(irn, i, get_n(irn, i, w->kind), NULL, w->kind, w->irg);
310 }
311
312 /**
313  * Pre-Walker: initializes the list-heads and set the out-count
314  * of all nodes to 0.
315  */
316 static void init_lh_walker(ir_node *irn, void *data) {
317         struct build_walker *w = data;
318         INIT_LIST_HEAD(_get_irn_outs_head(irn, w->kind));
319         _get_irn_edge_info(irn, w->kind)->out_count = 0;
320 }
321
322 /**
323  * Visitor: initializes the list-heads and set the out-count
324  * of all nodes to 0 of nodes that are not seen so far.
325  */
326 static void visitor(ir_node *irn, void *data) {
327         if (irn_not_visited(irn)) {
328                 mark_irn_visited(irn);
329                 init_lh_walker(irn, data);
330         }
331 }
332
333 /*
334  * Build the initial edge set.
335  * Beware, this is not a simple task because it suffers from two
336  * difficulties:
337  * - the anchor set allows access to Nodes that may not be reachable from
338  *   the End node
339  * - the identities add nodes to the "root set" that are not yet reachable
340  *   from End. However, after some transformations, the CSE may revival these
341  *   nodes
342  *
343  * These problems can be fixed using different strategies:
344  * - Add an age flag to every node. Whenever the edge of a node is older
345  *   then the current edge, invalidate the edges of this node.
346  *   While this would help for revivaled nodes, it increases memory and runtime.
347  * - Delete the identities set.
348  *   Solves the revival problem, but may increase the memory consumption, as
349  *   nodes cannot be revivaled at all.
350  * - Manually iterate over the identities root set. This did not consume more memory
351  *   but increase the computation time because the |identities| >= |V|
352  *
353  * Currently, we use the last option.
354  */
355 void edges_activate_kind(ir_graph *irg, ir_edge_kind_t kind)
356 {
357         struct build_walker w;
358         irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
359
360         w.irg  = irg;
361         w.kind = kind;
362
363         info->activated = 1;
364         edges_init_graph_kind(irg, kind);
365         irg_walk_graph(irg, init_lh_walker, build_edges_walker, &w);
366         irg_walk_anchors(irg, init_lh_walker, NULL, &w);
367         visit_all_identities(irg, visitor, &w);
368 }
369
370 void edges_deactivate_kind(ir_graph *irg, ir_edge_kind_t kind)
371 {
372         irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
373
374         info->activated = 0;
375         if (info->edges) {
376                 del_set(info->edges);
377                 info->edges = NULL;
378         }
379 }
380
381 int (edges_activated_kind)(const ir_graph *irg, ir_edge_kind_t kind)
382 {
383         return _edges_activated_kind(irg, kind);
384 }
385
386
387 /**
388  * Reroute all use-edges from a node to another.
389  * @param from The node whose use-edges shall be withdrawn.
390  * @param to   The node to which all the use-edges of @p from shall be
391  *             sent to.
392  * @param irg  The graph.
393  */
394 void edges_reroute_kind(ir_node *from, ir_node *to, ir_edge_kind_t kind, ir_graph *irg)
395 {
396         set_edge_func_t *set_edge = edge_kind_info[kind].set_edge;
397
398         if(set_edge && edges_activated_kind(irg, kind)) {
399                 struct list_head *head = _get_irn_outs_head(from, kind);
400
401                 DBG((dbg, LEVEL_5, "reroute from %+F to %+F\n", from, to));
402
403                 while(head != head->next) {
404                         ir_edge_t *edge = list_entry(head->next, ir_edge_t, list);
405                         assert(edge->pos >= -1);
406                         set_edge(edge->src, edge->pos, to);
407                 }
408         }
409 }
410
411 static void verify_set_presence(ir_node *irn, void *data)
412 {
413         struct build_walker *w = data;
414         set *edges             = _get_irg_edge_info(w->irg, w->kind)->edges;
415         int i, n;
416
417         foreach_tgt(irn, i, n, w->kind) {
418                 ir_edge_t templ, *e;
419
420                 templ.src = irn;
421                 templ.pos = i;
422
423                 e = set_find(edges, &templ, sizeof(templ), edge_hash(&templ));
424                 if(e != NULL)
425                         e->present = 1;
426                 else
427                         DBG((dbg, LEVEL_DEFAULT, "edge %+F,%d (kind: \"%s\") is missing\n", irn, i, get_kind_str(w->kind)));
428         }
429 }
430
431 static void verify_list_presence(ir_node *irn, void *data)
432 {
433         struct build_walker *w = data;
434         const ir_edge_t *e;
435
436         foreach_out_edge_kind(irn, e, w->kind) {
437                 ir_node *tgt = get_n(e->src, e->pos, w->kind);
438                 if(irn != tgt)
439                         DBG((dbg, LEVEL_DEFAULT, "edge %+F,%d (kind \"%s\") is no out edge of %+F but of %+F\n", e->src, e->pos, get_kind_str(w->kind), irn, tgt));
440         }
441 }
442
443 void edges_verify_kind(ir_graph *irg, ir_edge_kind_t kind)
444 {
445         struct build_walker w;
446         set *edges = _get_irg_edge_info(irg, kind)->edges;
447         ir_edge_t *e;
448
449         w.irg  = irg;
450         w.kind = kind;
451
452         /* Clear the present bit in all edges available. */
453         for(e = set_first(edges); e; e = set_next(edges))
454                 e->present = 0;
455
456         irg_walk_graph(irg, verify_set_presence, verify_list_presence, &w);
457
458         /*
459          * Dump all edges which are not invalid and not present.
460          * These edges are superfluous and their presence in the
461          * edge set is wrong.
462          */
463         for(e = set_first(edges); e; e = set_next(edges)) {
464                 if(!e->invalid && !e->present)
465                         DBG((dbg, LEVEL_DEFAULT, "edge %+F,%d is superfluous\n", e->src, e->pos));
466         }
467 }
468
469 void init_edges(void)
470 {
471         FIRM_DBG_REGISTER(dbg, DBG_EDGES);
472         /* firm_dbg_set_mask(dbg, -1); */
473 }
474
475 void edges_activate(ir_graph *irg)
476 {
477         edges_activate_kind(irg, EDGE_KIND_NORMAL);
478         edges_activate_kind(irg, EDGE_KIND_BLOCK);
479 }
480
481 void edges_deactivate(ir_graph *irg)
482 {
483         edges_deactivate_kind(irg, EDGE_KIND_NORMAL);
484         edges_deactivate_kind(irg, EDGE_KIND_BLOCK);
485 }
486
487 int edges_assure(ir_graph *irg)
488 {
489         int activated = edges_activated(irg);
490
491         if(!activated)
492                 edges_activate(irg);
493
494         return activated;
495 }
496
497 void edges_node_deleted(ir_node *irn, ir_graph *irg)
498 {
499         edges_node_deleted_kind(irn, EDGE_KIND_NORMAL, irg);
500         edges_node_deleted_kind(irn, EDGE_KIND_BLOCK, irg);
501 }
502
503
504 const ir_edge_t *(get_irn_out_edge_first_kind)(const ir_node *irn, ir_edge_kind_t kind)
505 {
506         return _get_irn_out_edge_first_kind(irn, kind);
507 }
508
509 const ir_edge_t *(get_irn_out_edge_next)(const ir_node *irn, const ir_edge_t *last)
510 {
511         return _get_irn_out_edge_next(irn, last);
512 }
513
514 ir_node *(get_edge_src_irn)(const ir_edge_t *edge)
515 {
516         return _get_edge_src_irn(edge);
517 }
518
519 int (get_edge_src_pos)(const ir_edge_t *edge)
520 {
521         return _get_edge_src_pos(edge);
522 }
523
524 int (get_irn_n_edges_kind)(const ir_node *irn, ir_edge_kind_t kind)
525 {
526         return _get_irn_n_edges_kind(irn, kind);
527 }
528
529 void dump_all_out_edges(ir_node *irn)
530 {
531         int i;
532         for(i = 0; i < EDGE_KIND_LAST; ++i) {
533                 const ir_edge_t *edge;
534
535                 printf("kind \"%s\"\n", get_kind_str(i));
536                 foreach_out_edge_kind(irn, edge, i) {
537                         ir_printf("\t%+F(%d)\n", edge->src, edge->pos);
538                 }
539         }
540 }