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