f2795cbee7b638b9ecb009481c213fdbb7578dcf
[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         assert(node_is_in_irgs_storage(irg, src) && "source not in irg");
107
108         /*
109          * Only do something, if the old and new target differ.
110          */
111         if(tgt != old_tgt) {
112                 int is_block_edge = is_Block(src);
113                 set *edges = _get_irg_edge_info(irg)->edges;
114                 ir_block_edge_t space;
115                 ir_edge_t *templ = (ir_edge_t *) &space;
116                 ir_edge_t *edge;
117                 size_t size;
118
119     /*
120      * This is scray, but:
121      * If two entries in a set do not have the same size, they are
122      * treated as unequal, ignoring the comparison function.
123      * So, edges from blocks have extra storage (they are
124      * ir_block_edge_t's).
125                  *
126                  * Also add the amount of registered private data to the
127                  * size of the edge.
128      */
129     size = edges_private_size +
130                         is_block_edge ? sizeof(ir_block_edge_t) : sizeof(ir_edge_t);
131
132                 /* Initialize the edge template to search in the set. */
133     memset(templ, 0, size);
134 #ifdef DEBUG_libfirm
135                 templ->src_nr = get_irn_node_nr(src);
136 #endif
137                 templ->src = src;
138                 templ->pos = pos;
139                 templ->invalid = 0;
140                 templ->present = 0;
141
142                 /*
143                  * If the target is NULL, the edge shall be deleted.
144                  */
145                 if(tgt == NULL) {
146                         /* search the edge in the set. */
147                         edge = set_find(edges, templ, size, edge_hash(templ));
148
149                         /* mark the edge invalid if it was found */
150                         if(edge) {
151         ir_block_edge_t *block_edge = (ir_block_edge_t *) edge;
152
153                                 msg = "deleting";
154                                 list_del(&edge->list);
155                                 edge->invalid = 1;
156                                 edge->pos = -2;
157                                 edge->src = NULL;
158
159         /*
160          * If the edge is a cf edge, we delete it also
161          * from the list of all block successor edges.
162          */
163         if(is_block_edge)
164           list_del(&block_edge->succ_list);
165                         }
166
167                         /* If the edge was not found issue a warning on the debug stream */
168                         else {
169                                 msg = "edge to delete not found!\n";
170                         }
171                 } /* if */
172
173                 /*
174                  * The target is not NULL and the old target differs
175                  * from the new target, the edge shall be moved (if the
176                  * old target was != NULL) or added (if the old target was
177                  * NULL).
178                  */
179                 else {
180                         struct list_head *head = _get_irn_outs_head(tgt);
181
182       /*
183        * The list head in the block of the edges target.
184        * Therein all control flow edges directed at that block
185        * are recorded.
186        */
187       struct list_head *succ_head =
188         is_block_edge ? _get_block_succ_head(get_nodes_block(tgt)) : NULL;
189
190       ir_block_edge_t *block_edge;
191
192                         if(!node_is_in_irgs_storage(irg, tgt))
193                                 return;
194
195                         assert(head->next && head->prev &&
196                                         "target list head must have been initialized");
197
198                         /*
199                          * insert the edge, if it is not yet in the set or return
200                          * the instance in the set.
201                          */
202                         edge = set_insert(edges, templ, size, edge_hash(templ));
203       block_edge = (ir_block_edge_t *) edge;
204
205 #ifdef DEBUG_libfirm
206                         assert(!edge->invalid && "Invalid edge encountered");
207 #endif
208
209                         /* If the old target is not null, the edge is moved. */
210                         if(old_tgt) {
211                                 msg = "redirecting";
212                                 list_move(&edge->list, head);
213
214         /* If the edge is a cf edge, move it from the successor list. */
215         if(is_block_edge)
216           list_move(&block_edge->succ_list, succ_head);
217
218                                 _get_irn_edge_info(old_tgt)->out_count -= 1;
219                         }
220
221                         /* The old target was null, thus, the edge is newly created. */
222                         else {
223                                 msg = "adding";
224                                 list_add(&edge->list, head);
225
226         /*
227          * If the edge is cf edge, enter it into the successor list
228          * of the target node's block.
229          */
230         if(is_block_edge)
231           list_add(&block_edge->succ_list, succ_head);
232                         }
233
234                         _get_irn_edge_info(tgt)->out_count += 1;
235                 } /* else */
236         }
237
238         /* If the target and the old target are equal, nothing is done. */
239         DBG((dbg, LEVEL_5, "announce out edge: %n[%p] %d-> %n[%p](%n[%p]): %s\n",
240                                 src, src, pos, tgt, tgt, old_tgt, old_tgt, msg));
241 }
242
243 void edges_node_deleted(ir_node *old, ir_graph *irg)
244 {
245         if(edges_activated(irg)) {
246                 int not_a_block = !is_Block(old);
247                 ir_edge_t templ;
248                 int i, n;
249
250                 templ.src = old;
251                 DBG((dbg, LEVEL_5, "node deleted: %n\n", old));
252
253                 /* Change to get_irn_n */
254                 for(i = -not_a_block, n = get_irn_arity(old); i < n; ++i) {
255                         ir_node *old_tgt = get_irn_n(old, i);
256                         DBG((dbg, LEVEL_5, "\tdelete to old target %n\n", old_tgt));
257                         edges_notify_edge(old, i, NULL, old_tgt, irg);
258                 }
259
260         }
261 }
262
263 void edges_invalidate(ir_node *irn, ir_graph *irg)
264 {
265         edges_node_deleted(irn, irg);
266 }
267
268 static void build_edges_walker(ir_node *irn, void *data)
269 {
270         ir_graph *irg = data;
271         int not_a_block = !is_Block(irn);
272         int i, n;
273
274         for(i = -not_a_block, n = get_irn_arity(irn); i < n; ++i)
275                 edges_notify_edge(irn, i, get_irn_n(irn, i), NULL, irg);
276 }
277
278 static void init_lh_walker(ir_node *irn, void *data)
279 {
280         INIT_LIST_HEAD(_get_irn_outs_head(irn));
281         if(is_Block(irn))
282                 INIT_LIST_HEAD(_get_block_succ_head(irn));
283 }
284
285 void edges_activate(ir_graph *irg)
286 {
287         irg_edge_info_t *info = _get_irg_edge_info(irg);
288
289         info->activated = 1;
290         edges_init_graph(irg);
291         irg_walk_graph(irg, init_lh_walker, build_edges_walker, irg);
292 }
293
294 void edges_deactivate(ir_graph *irg)
295 {
296         irg_edge_info_t *info = _get_irg_edge_info(irg);
297
298         info->activated = 0;
299         if(info->edges) {
300                 del_set(info->edges);
301     info->edges = NULL;
302   }
303 }
304
305 int (edges_activated)(const ir_graph *irg)
306 {
307         return _edges_activated(irg);
308 }
309
310
311 /**
312  * Reroute all use-edges from a node to another.
313  * @param from The node whose use-edges shall be withdrawn.
314  * @param to The node to which all the use-edges of @p from shall be
315  * sent to.
316  */
317 void edges_reroute(ir_node *from, ir_node *to, ir_graph *irg)
318 {
319         if(edges_activated(irg)) {
320                 struct list_head *head = _get_irn_outs_head(from);
321
322                 DBG((firm_dbg_register(DBG_EDGES), LEVEL_5,
323                                         "reroute from %n to %n\n", from, to));
324
325                 while(head != head->next) {
326                         ir_edge_t *edge = list_entry(head->next, ir_edge_t, list);
327                         // DBG((dbg, LEVEL_5, "\t%n %d\n", edge->src, edge->pos));
328                         assert(edge->pos >= -1);
329                         set_irn_n(edge->src, edge->pos, to);
330                 }
331         }
332 }
333
334 static void verify_set_presence(ir_node *irn, void *data)
335 {
336         ir_graph *irg = data;
337         set *edges = _get_irg_edge_info(irg)->edges;
338         int not_a_block = !is_Block(irn);
339         int i, n;
340
341         for(i = 0, n = get_irn_arity(irn) + not_a_block; i < n; ++i) {
342     ir_block_edge_t space;
343                 ir_edge_t *templ = (ir_edge_t *) &space;
344                 ir_edge_t *e;
345     size_t size = not_a_block ? sizeof(ir_edge_t) : sizeof(ir_block_edge_t);
346
347                 templ->src = irn;
348                 templ->pos = i - not_a_block;
349
350                 e = set_find(edges, templ, size, edge_hash(templ));
351                 if(e != NULL)
352                         e->present = 1;
353                 else
354                         DBG((dbg, LEVEL_DEFAULT, "edge %n,%d is missing\n", irn, templ->pos));
355         }
356 }
357
358 static void verify_list_presence(ir_node *irn, void *data)
359 {
360         const ir_edge_t *e;
361
362         foreach_out_edge(irn, e) {
363                 ir_node *tgt = get_irn_n(e->src, e->pos);
364                 if(irn != tgt)
365                         DBG((dbg, LEVEL_DEFAULT, "edge %n,%d is no out edge of %n but of %n\n",
366                                         e->src, e->pos, irn, tgt));
367         }
368
369 }
370
371 void edges_verify(ir_graph *irg)
372 {
373         set *edges = _get_irg_edge_info(irg)->edges;
374         ir_edge_t *e;
375
376         /* Clear the present bit in all edges available. */
377         for(e = set_first(edges); e; e = set_next(edges))
378                 e->present = 0;
379
380         irg_walk_graph(irg, verify_set_presence, verify_list_presence, irg);
381
382         /*
383          * Dump all edges which are not invalid and not present.
384          * These edges are superfluous and their presence in the
385          * edge set is wrong.
386          */
387         for(e = set_first(edges); e; e = set_next(edges)) {
388                 if(!e->invalid && !e->present)
389                         DBG((dbg, LEVEL_DEFAULT, "edge %n,%d is superfluous\n", e->src, e->pos));
390         }
391 }
392
393 void init_edges(void)
394 {
395         dbg = firm_dbg_register(DBG_EDGES);
396         /* firm_dbg_set_mask(dbg, -1); */
397 }
398
399
400 const ir_edge_t *(get_irn_out_edge_first)(const ir_node *irn)
401 {
402         return _get_irn_out_edge_first(irn);
403 }
404
405 const ir_edge_t *(get_irn_out_edge_next)(const ir_node *irn, const ir_edge_t *last)
406 {
407         return _get_irn_out_edge_next(irn, last);
408 }
409
410 ir_node *(get_edge_src_irn)(const ir_edge_t *edge)
411 {
412         return _get_edge_src_irn(edge);
413 }
414
415 int (get_edge_src_pos)(const ir_edge_t *edge)
416 {
417         return _get_edge_src_pos(edge);
418 }
419
420 int (get_irn_n_edges)(const ir_node *irn)
421 {
422   return _get_irn_n_edges(irn);
423 }
424
425
426 #endif /* FIRM_EDGES_INPLACE */