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