Added support for out edges.
[libfirm] / ir / ir / iredges.c
1 /**
2  * Always available outs.
3  * @author Sebastian Hack
4  * @date 14.1.2005
5  */
6
7 #include "irnode_t.h"
8 #include "iredges_t.h"
9 #include "irdump_t.h"
10 #include "irprintf.h"
11 #include "irhooks.h"
12 #include "debug.h"
13 #include "set.h"
14
15 static firm_dbg_module_t *dbg;
16
17 #define TIMES37(x) (((x) << 5) + ((x) << 2) + (x))
18
19 #define get_irn_out_list_head(irn) (&get_irn_out_info(irn)->outs)
20
21 static int edge_cmp(const void *p1, const void *p2, size_t len)
22 {
23         const ir_edge_t *e1 = p1;
24         const ir_edge_t *e2 = p2;
25         int res = e1->src == e2->src && e1->pos == e2->pos;
26
27         return !res;
28 }
29
30 static INLINE unsigned edge_hash(const ir_edge_t *edge)
31 {
32         unsigned result = HASH_PTR(edge->src);
33         result = TIMES37(result) + edge->pos;
34         return result;
35 }
36
37 /**
38  * Initialize the out information for a graph.
39  * @note Dead node elim can call this on an already initialized graph.
40  */
41 void edges_init_graph(ir_graph *irg)
42 {
43         if(edges_activated(irg)) {
44                 irg_edge_info_t *info = _get_irg_edge_info(irg);
45                 int amount = 2048;
46
47                 if(info->edges) {
48                         amount = set_count(info->edges);
49                         del_set(info->edges);
50                 }
51
52                 info->edges = new_set(edge_cmp, amount);
53         }
54 }
55
56 void edges_notify_edge(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt, ir_graph *irg)
57 {
58         const char *msg = "";
59
60         if(!edges_activated(irg))
61                 return;
62
63         assert(node_is_in_irgs_storage(irg, src) && "source not in irg");
64
65         /*
66          * Only do something, if the old and new target differ.
67          */
68         if(tgt != old_tgt) {
69                 set *edges = _get_irg_edge_info(irg)->edges;
70                 ir_edge_t templ;
71                 ir_edge_t *edge;
72
73                 /* Initialize the edge template to search in the set. */
74 #ifdef DEBUG_libfirm
75                 templ.src_nr = get_irn_node_nr(src);
76 #endif
77                 templ.src = src;
78                 templ.pos = pos;
79                 templ.invalid = 0;
80                 templ.present = 0;
81                 INIT_LIST_HEAD(&templ.list);
82
83                 /*
84                  * If the target is NULL, the edge shall be deleted.
85                  */
86                 if(tgt == NULL) {
87                         /* search the edge in the set. */
88                         edge = set_find(edges, &templ, sizeof(templ), edge_hash(&templ));
89
90                         /* mark the edge invalid if it was found */
91                         if(edge) {
92                                 msg = "deleting";
93                                 list_del(&edge->list);
94                                 edge->invalid = 1;
95                                 edge->pos = -2;
96                                 edge->src = NULL;
97                         }
98
99                         /* If the edge was not found issue a warning on the debug stream */
100                         else {
101                                 msg = "edge to delete not found!\n";
102                         }
103                 } /* if */
104
105                 /*
106                  * The target is not NULL and the old target differs
107                  * from the new target, the edge shall be moved (if the
108                  * old target was != NULL) or added (if the old target was
109                  * NULL).
110                  */
111                 else {
112                         struct list_head *head = _get_irn_outs_head(tgt);
113
114                         if(!node_is_in_irgs_storage(irg, tgt))
115                                 return;
116
117                         assert(head->next && head->prev &&
118                                         "target list head must have been initialized");
119
120                         /*
121                          * insert the edge, if it is not yet in the set or return
122                          * the instance in the set.
123                          */
124                         edge = set_insert(edges, &templ, sizeof(templ), edge_hash(&templ));
125
126 #ifdef DEBUG_libfirm
127                         assert(!edge->invalid && "Invalid edge encountered");
128 #endif
129
130                         /* If the old target is not null, the edge is moved. */
131                         if(old_tgt) {
132                                 msg = "redirecting";
133                                 list_move(&edge->list, head);
134                                 _get_irn_edge_info(old_tgt)->out_count -= 1;
135                         }
136
137                         /* The old target was null, thus, the edge is newly created. */
138                         else {
139                                 msg = "adding";
140                                 list_add(&edge->list, head);
141                         }
142
143                         _get_irn_edge_info(tgt)->out_count += 1;
144                 } /* else */
145         }
146
147         /* If the target and the old target are equal, nothing is done. */
148         DBG((dbg, LEVEL_5, "announce out edge: %n[%p] %d-> %n[%p](%n[%p]): %s\n",
149                                 src, src, pos, tgt, tgt, old_tgt, old_tgt, msg));
150 }
151
152 void edges_node_deleted(ir_node *old, ir_graph *irg)
153 {
154         if(edges_activated(irg)) {
155                 int not_a_block = !is_Block(old);
156                 ir_edge_t templ;
157                 int i, n;
158
159                 templ.src = old;
160                 DBG((dbg, LEVEL_5, "node deleted: %n\n", old));
161
162                 /* Change to get_irn_n */
163                 for(i = -not_a_block, n = get_irn_arity(old); i < n; ++i) {
164                         ir_node *old_tgt = get_irn_n(old, i);
165                         DBG((dbg, LEVEL_5, "\tdelete to old target %n\n", old_tgt));
166                         edges_notify_edge(old, i, NULL, old_tgt, irg);
167                 }
168
169         }
170 }
171
172 void edges_invalidate(ir_node *irn, ir_graph *irg)
173 {
174         edges_node_deleted(irn, irg);
175 }
176
177
178 static void build_edges_walker(ir_node *irn, void *data)
179 {
180         ir_graph *irg = data;
181         int not_a_block = !is_Block(irn);
182         int i, n;
183
184         for(i = -not_a_block, n = get_irn_arity(irn); i < n; ++i)
185                 edges_notify_edge(irn, i, get_irn_n(irn, i), NULL, irg);
186 }
187
188 void edges_activate(ir_graph *irg)
189 {
190         irg_edge_info_t *info = _get_irg_edge_info(irg);
191
192         info->activated = 1;
193         edges_init_graph(irg);
194         irg_walk_graph(irg, NULL, build_edges_walker, irg);
195 }
196
197 void edges_deactivate(ir_graph *irg)
198 {
199         irg_edge_info_t *info = _get_irg_edge_info(irg);
200
201         info->activated = 0;
202         if(info->edges)
203                 del_set(info->edges);
204 }
205
206 int (edges_activated)(const ir_graph *irg)
207 {
208         return _edges_activated(irg);
209 }
210
211
212 /**
213  * Reroute all use-edges from a node to another.
214  * @param from The node whose use-edges shall be withdrawn.
215  * @param to The node to which all the use-edges of @p from shall be
216  * sent to.
217  */
218 void edges_reroute(ir_node *from, ir_node *to, ir_graph *irg)
219 {
220         if(edges_activated(irg)) {
221                 struct list_head *head = _get_irn_outs_head(from);
222
223                 DBG((firm_dbg_register(DBG_EDGES), LEVEL_5,
224                                         "reroute from %n to %n\n", from, to));
225
226                 while(head != head->next) {
227                         ir_edge_t *edge = list_entry(head->next, ir_edge_t, list);
228                         // DBG((dbg, LEVEL_5, "\t%n %d\n", edge->src, edge->pos));
229                         assert(edge->pos >= -1);
230                         set_irn_n(edge->src, edge->pos, to);
231                 }
232         }
233 }
234
235 static void verify_set_presence(ir_node *irn, void *data)
236 {
237         ir_graph *irg = data;
238         set *edges = _get_irg_edge_info(irg)->edges;
239         int not_a_block = !is_Block(irn);
240         int i, n;
241
242         for(i = 0, n = get_irn_arity(irn) + not_a_block; i < n; ++i) {
243                 ir_edge_t templ;
244                 ir_edge_t *e;
245
246                 templ.src = irn;
247                 templ.pos = i - not_a_block;
248
249                 e = set_find(edges, &templ, sizeof(templ), edge_hash(&templ));
250                 if(e != NULL)
251                         e->present = 1;
252                 else
253                         ir_fprintf(stderr, "edge %n,%d is missing\n", irn, templ.pos);
254         }
255 }
256
257 static void verify_list_presence(ir_node *irn, void *data)
258 {
259         const ir_edge_t *e;
260
261         foreach_out_edge(irn, e) {
262                 ir_node *tgt = get_irn_n(e->src, e->pos);
263                 if(irn != tgt)
264                         ir_fprintf(stderr, "edge %n,%d is no out edge of %n but of %n\n",
265                                         e->src, e->pos, irn, tgt);
266         }
267
268 }
269
270 void edges_verify(ir_graph *irg)
271 {
272         set *edges = _get_irg_edge_info(irg)->edges;
273         ir_edge_t *e;
274
275         /* Clear the present bit in all edges available. */
276         for(e = set_first(edges); e; e = set_next(edges))
277                 e->present = 0;
278
279         irg_walk_graph(irg, verify_set_presence, verify_list_presence, irg);
280
281         /*
282          * Dump all edges which are not invalid and not present.
283          * These edges are superfluous and their presence in the
284          * edge set is wrong.
285          */
286         for(e = set_first(edges); e; e = set_next(edges)) {
287                 if(!e->invalid && !e->present)
288                         ir_fprintf(stderr, "edge %n,%d is superfluous\n", e->src, e->pos);
289         }
290 }
291
292 void init_edges(void)
293 {
294         dbg = firm_dbg_register(DBG_EDGES);
295         /* firm_dbg_set_mask(dbg, -1); */
296 }
297
298
299 const ir_edge_t *(get_irn_out_edge_first)(const ir_node *irn)
300 {
301         return _get_irn_out_edge_first(irn);
302 }
303
304 const ir_edge_t *(get_irn_out_edge_next)(const ir_node *irn, const ir_edge_t *last)
305 {
306         return _get_irn_out_edge_next(irn, last);
307 }
308
309 ir_node *(get_edge_src_irn)(const ir_edge_t *edge)
310 {
311         return _get_edge_src_irn(edge);
312 }
313
314 int (get_edge_src_pos)(const ir_edge_t *edge)
315 {
316         return _get_edge_src_pos(edge);
317 }