Dump calling conventions for entities
[libfirm] / ir / ir / iredges.c
1 /**
2  * Always available outs.
3  * @author Sebastian Hack
4  * @date 14.1.2005
5  */
6
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #ifdef HAVE_ALLOCA_H
12 #include <alloca.h>
13 #endif
14 #ifdef HAVE_MALLOC_H
15 #include <malloc.h>
16 #endif
17
18 #include "irnode_t.h"
19 #include "iredges_t.h"
20 #include "irdump_t.h"
21 #include "irprintf.h"
22 #include "irhooks.h"
23 #include "debug.h"
24 #include "set.h"
25
26 static firm_dbg_module_t *dbg;
27
28 #if FIRM_EDGES_INPLACE
29
30 /**
31  * This flag is set to 1, if the edges get initialized for an irg.
32  * Then register additional data is forbidden.
33  */
34 static int edges_used = 0;
35
36 static int edges_private_size = 0;
37
38 int edges_register_private_data(size_t n)
39 {
40         int res = edges_private_size;
41
42         assert(!edges_used && "you cannot register private edge data, if edges have been initialized");
43
44         edges_private_size += n;
45         return res;
46 }
47
48 #define TIMES37(x) (((x) << 5) + ((x) << 2) + (x))
49
50 #define get_irn_out_list_head(irn) (&get_irn_out_info(irn)->outs)
51
52 static int edge_cmp(const void *p1, const void *p2, size_t len)
53 {
54         const ir_edge_t *e1 = p1;
55         const ir_edge_t *e2 = p2;
56         int res = e1->src == e2->src && e1->pos == e2->pos;
57
58         return !res;
59 }
60
61 #define edge_hash(edge) (TIMES37((edge)->pos) + HASH_PTR((edge)->src))
62
63 /**
64  * Initialize the out information for a graph.
65  * @note Dead node elim can call this on an already initialized graph.
66  */
67 void edges_init_graph(ir_graph *irg)
68 {
69         if(edges_activated(irg)) {
70                 irg_edge_info_t *info = _get_irg_edge_info(irg);
71                 int amount = 2048;
72
73                 edges_used = 1;
74
75                 if(info->edges) {
76                         amount = set_count(info->edges);
77                         del_set(info->edges);
78                 }
79
80                 info->edges = new_set(edge_cmp, amount);
81         }
82 }
83
84 #define EDGE_SIZE(src) \
85     (edges_private_size + (is_Block(src) ? sizeof(ir_block_edge_t) : sizeof(ir_edge_t)))
86
87
88 /**
89  * Get the edge object of an outgoing edge at a node.
90  * @param   irg The graph, the node is in.
91  * @param   src The node at which the edge originates.
92  * @param   pos The position of the edge.
93  * @return      The corresponding edge object or NULL,
94  *              if no such edge exists.
95  */
96 const ir_edge_t *get_irn_edge(ir_graph *irg, const ir_node *src, int pos)
97 {
98         if(edges_activated(irg)) {
99                 irg_edge_info_t *info = _get_irg_edge_info(irg);
100                 size_t size           = EDGE_SIZE(src);
101                 ir_edge_t *templ      = alloca(size);
102
103                 memset(templ, 0, size);
104                 templ->src = (ir_node *) src;
105                 templ->pos = pos;
106                 return set_find(info->edges, templ, size, edge_hash(templ));
107         }
108
109         return NULL;
110 }
111
112 void edges_notify_edge(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt, ir_graph *irg)
113 {
114         const char *msg = "";
115
116         if(!edges_activated(irg))
117                 return;
118
119 #if 0
120         assert(node_is_in_irgs_storage(irg, src) && "source not in irg");
121 #endif
122
123         /*
124          * Only do something, if the old and new target differ.
125          */
126         if(tgt != old_tgt) {
127                 int is_block_edge = is_Block(src);
128                 set *edges = _get_irg_edge_info(irg)->edges;
129                 ir_edge_t *edge;
130
131                 /*
132                  * This is scray, but:
133                  * If two entries in a set do not have the same size, they are
134                  * treated as unequal, ignoring the comparison function.
135                  * So, edges from blocks have extra storage (they are
136                  * ir_block_edge_t's).
137                  *
138                  * Also add the amount of registered private data to the
139                  * size of the edge.
140                  */
141                 size_t size      = EDGE_SIZE(src);
142                 ir_edge_t *templ = alloca(size);
143
144                 /* Initialize the edge template to search in the set. */
145                 memset(templ, 0, size);
146 #ifdef DEBUG_libfirm
147                 templ->src_nr = get_irn_node_nr(src);
148 #endif
149                 templ->src = src;
150                 templ->pos = pos;
151                 templ->invalid = 0;
152                 templ->present = 0;
153
154                 /*
155                  * If the target is NULL, the edge shall be deleted.
156                  */
157                 if(tgt == NULL) {
158                         /* search the edge in the set. */
159                         edge = set_find(edges, templ, size, edge_hash(templ));
160
161                         /* mark the edge invalid if it was found */
162                         if(edge) {
163                                 ir_block_edge_t *block_edge = (ir_block_edge_t *) edge;
164
165                                 msg = "deleting";
166                                 list_del(&edge->list);
167                                 edge->invalid = 1;
168                                 edge->pos = -2;
169                                 edge->src = NULL;
170
171                                 /*
172                                  * If the edge is a cf edge, we delete it also
173                                  * from the list of all block successor edges.
174                                  */
175                                 if(is_block_edge)
176                                         list_del(&block_edge->succ_list);
177                         }
178
179                         /* If the edge was not found issue a warning on the debug stream */
180                         else {
181                                 msg = "edge to delete not found!\n";
182                         }
183                 } /* if */
184
185                 /*
186                  * The target is not NULL and the old target differs
187                  * from the new target, the edge shall be moved (if the
188                  * old target was != NULL) or added (if the old target was
189                  * NULL).
190                  */
191                 else {
192                         struct list_head *head = _get_irn_outs_head(tgt);
193
194                         /*
195                          * The list head in the block of the edges target.
196                          * Therein all control flow edges directed at that block
197                          * are recorded.
198                          */
199                         struct list_head *succ_head =
200                                 is_block_edge ? _get_block_succ_head(get_nodes_block(tgt)) : NULL;
201
202                         ir_block_edge_t *block_edge;
203
204 #if 0
205                         if(!node_is_in_irgs_storage(irg, tgt))
206                                 return;
207 #endif
208                         assert(head->next && head->prev &&
209                                         "target list head must have been initialized");
210
211                         /*
212                          * insert the edge, if it is not yet in the set or return
213                          * the instance in the set.
214                          */
215                         edge = set_insert(edges, templ, size, edge_hash(templ));
216                         block_edge = (ir_block_edge_t *) edge;
217
218 #ifdef DEBUG_libfirm
219                         assert(!edge->invalid && "Invalid edge encountered");
220 #endif
221
222                         /* If the old target is not null, the edge is moved. */
223                         if(old_tgt) {
224                                 msg = "redirecting";
225                                 list_move(&edge->list, head);
226
227                                 /* If the edge is a cf edge, move it from the successor list. */
228                                 if(is_block_edge)
229                                         list_move(&block_edge->succ_list, succ_head);
230
231                                 _get_irn_edge_info(old_tgt)->out_count -= 1;
232                         }
233
234                         /* The old target was null, thus, the edge is newly created. */
235                         else {
236                                 msg = "adding";
237                                 list_add(&edge->list, head);
238
239                                 /*
240                                  * If the edge is cf edge, enter it into the successor list
241                                  * of the target node's block.
242                                  */
243                                 if(is_block_edge)
244                                         list_add(&block_edge->succ_list, succ_head);
245                         }
246
247                         _get_irn_edge_info(tgt)->out_count += 1;
248                 } /* else */
249         }
250
251         /* If the target and the old target are equal, nothing is done. */
252         DBG((dbg, LEVEL_5, "announce out edge: %n[%p] %d-> %n[%p](%n[%p]): %s\n",
253                                 src, src, pos, tgt, tgt, old_tgt, old_tgt, msg));
254 }
255
256 void edges_node_deleted(ir_node *old, ir_graph *irg)
257 {
258         if(edges_activated(irg)) {
259                 int not_a_block = !is_Block(old);
260                 ir_edge_t templ;
261                 int i, n;
262
263                 templ.src = old;
264                 DBG((dbg, LEVEL_5, "node deleted: %n\n", old));
265
266                 /* Change to get_irn_n */
267                 for(i = -not_a_block, n = get_irn_arity(old); i < n; ++i) {
268                         ir_node *old_tgt = get_irn_n(old, i);
269                         DBG((dbg, LEVEL_5, "\tdelete to old target %n\n", old_tgt));
270                         edges_notify_edge(old, i, NULL, old_tgt, irg);
271                 }
272
273         }
274 }
275
276 void edges_invalidate(ir_node *irn, ir_graph *irg)
277 {
278         edges_node_deleted(irn, irg);
279 }
280
281 static void build_edges_walker(ir_node *irn, void *data)
282 {
283         ir_graph *irg = data;
284         int not_a_block = !is_Block(irn);
285         int i, n;
286
287         for(i = -not_a_block, n = get_irn_arity(irn); i < n; ++i)
288                 edges_notify_edge(irn, i, get_irn_n(irn, i), NULL, irg);
289 }
290
291 static void init_lh_walker(ir_node *irn, void *data)
292 {
293         INIT_LIST_HEAD(_get_irn_outs_head(irn));
294         if(is_Block(irn))
295                 INIT_LIST_HEAD(_get_block_succ_head(irn));
296 }
297
298 void edges_activate(ir_graph *irg)
299 {
300         irg_edge_info_t *info = _get_irg_edge_info(irg);
301
302         info->activated = 1;
303         edges_init_graph(irg);
304         irg_walk_graph(irg, init_lh_walker, build_edges_walker, irg);
305 }
306
307 void edges_deactivate(ir_graph *irg)
308 {
309         irg_edge_info_t *info = _get_irg_edge_info(irg);
310
311         info->activated = 0;
312         if(info->edges) {
313                 del_set(info->edges);
314     info->edges = NULL;
315   }
316 }
317
318 int (edges_activated)(const ir_graph *irg)
319 {
320         return _edges_activated(irg);
321 }
322
323
324 /**
325  * Reroute all use-edges from a node to another.
326  * @param from The node whose use-edges shall be withdrawn.
327  * @param to The node to which all the use-edges of @p from shall be
328  * sent to.
329  */
330 void edges_reroute(ir_node *from, ir_node *to, ir_graph *irg)
331 {
332         if(edges_activated(irg)) {
333                 struct list_head *head = _get_irn_outs_head(from);
334
335                 DBG((firm_dbg_register(DBG_EDGES), LEVEL_5,
336                                         "reroute from %n to %n\n", from, to));
337
338                 while(head != head->next) {
339                         ir_edge_t *edge = list_entry(head->next, ir_edge_t, list);
340                         // DBG((dbg, LEVEL_5, "\t%n %d\n", edge->src, edge->pos));
341                         assert(edge->pos >= -1);
342                         set_irn_n(edge->src, edge->pos, to);
343                 }
344         }
345 }
346
347 static void verify_set_presence(ir_node *irn, void *data)
348 {
349         ir_graph *irg = data;
350         set *edges = _get_irg_edge_info(irg)->edges;
351         int not_a_block = !is_Block(irn);
352         int i, n;
353
354         for(i = 0, n = get_irn_arity(irn) + not_a_block; i < n; ++i) {
355     ir_block_edge_t space;
356                 ir_edge_t *templ = (ir_edge_t *) &space;
357                 ir_edge_t *e;
358     size_t size = not_a_block ? sizeof(ir_edge_t) : sizeof(ir_block_edge_t);
359
360                 templ->src = irn;
361                 templ->pos = i - not_a_block;
362
363                 e = set_find(edges, templ, size, edge_hash(templ));
364                 if(e != NULL)
365                         e->present = 1;
366                 else
367                         DBG((dbg, LEVEL_DEFAULT, "edge %n,%d is missing\n", irn, templ->pos));
368         }
369 }
370
371 static void verify_list_presence(ir_node *irn, void *data)
372 {
373         const ir_edge_t *e;
374
375         foreach_out_edge(irn, e) {
376                 ir_node *tgt = get_irn_n(e->src, e->pos);
377                 if(irn != tgt)
378                         DBG((dbg, LEVEL_DEFAULT, "edge %n,%d is no out edge of %n but of %n\n",
379                                         e->src, e->pos, irn, tgt));
380         }
381
382 }
383
384 void edges_verify(ir_graph *irg)
385 {
386         set *edges = _get_irg_edge_info(irg)->edges;
387         ir_edge_t *e;
388
389         /* Clear the present bit in all edges available. */
390         for(e = set_first(edges); e; e = set_next(edges))
391                 e->present = 0;
392
393         irg_walk_graph(irg, verify_set_presence, verify_list_presence, irg);
394
395         /*
396          * Dump all edges which are not invalid and not present.
397          * These edges are superfluous and their presence in the
398          * edge set is wrong.
399          */
400         for(e = set_first(edges); e; e = set_next(edges)) {
401                 if(!e->invalid && !e->present)
402                         DBG((dbg, LEVEL_DEFAULT, "edge %n,%d is superfluous\n", e->src, e->pos));
403         }
404 }
405
406 void init_edges(void)
407 {
408         dbg = firm_dbg_register(DBG_EDGES);
409         /* firm_dbg_set_mask(dbg, -1); */
410 }
411
412
413 const ir_edge_t *(get_irn_out_edge_first)(const ir_node *irn)
414 {
415         return _get_irn_out_edge_first(irn);
416 }
417
418 const ir_edge_t *(get_irn_out_edge_next)(const ir_node *irn, const ir_edge_t *last)
419 {
420         return _get_irn_out_edge_next(irn, last);
421 }
422
423 ir_node *(get_edge_src_irn)(const ir_edge_t *edge)
424 {
425         return _get_edge_src_irn(edge);
426 }
427
428 int (get_edge_src_pos)(const ir_edge_t *edge)
429 {
430         return _get_edge_src_pos(edge);
431 }
432
433 int (get_irn_n_edges)(const ir_node *irn)
434 {
435   return _get_irn_n_edges(irn);
436 }
437
438
439 #endif /* FIRM_EDGES_INPLACE */