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