2 * Always available outs.
3 * @author Sebastian Hack
12 #include "iredges_t.h"
19 static firm_dbg_module_t *dbg;
21 #if FIRM_EDGES_INPLACE
23 #define TIMES37(x) (((x) << 5) + ((x) << 2) + (x))
25 #define get_irn_out_list_head(irn) (&get_irn_out_info(irn)->outs)
27 static int edge_cmp(const void *p1, const void *p2, size_t len)
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;
36 static INLINE unsigned edge_hash(const ir_edge_t *edge)
38 unsigned result = HASH_PTR(edge->src);
39 result = TIMES37(result) + edge->pos;
44 * Initialize the out information for a graph.
45 * @note Dead node elim can call this on an already initialized graph.
47 void edges_init_graph(ir_graph *irg)
49 if(edges_activated(irg)) {
50 irg_edge_info_t *info = _get_irg_edge_info(irg);
54 amount = set_count(info->edges);
58 info->edges = new_set(edge_cmp, amount);
62 void edges_notify_edge(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt, ir_graph *irg)
66 if(!edges_activated(irg))
69 assert(node_is_in_irgs_storage(irg, src) && "source not in irg");
72 * Only do something, if the old and new target differ.
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;
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
89 size = is_block_edge ? sizeof(ir_block_edge_t) : sizeof(ir_edge_t);
91 /* Initialize the edge template to search in the set. */
92 memset(templ, 0, size);
94 templ->src_nr = get_irn_node_nr(src);
102 * If the target is NULL, the edge shall be deleted.
105 /* search the edge in the set. */
106 edge = set_find(edges, templ, size, edge_hash(templ));
108 /* mark the edge invalid if it was found */
110 ir_block_edge_t *block_edge = (ir_block_edge_t *) edge;
113 list_del(&edge->list);
119 * If the edge is a cf edge, we delete it also
120 * from the list of all block successor edges.
123 list_del(&block_edge->succ_list);
126 /* If the edge was not found issue a warning on the debug stream */
128 msg = "edge to delete not found!\n";
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
139 struct list_head *head = _get_irn_outs_head(tgt);
142 * The list head in the block of the edges target.
143 * Therein all control flow edges directed at that block
146 struct list_head *succ_head =
147 is_block_edge ? _get_block_succ_head(get_nodes_block(tgt)) : NULL;
149 ir_block_edge_t *block_edge;
151 if(!node_is_in_irgs_storage(irg, tgt))
154 assert(head->next && head->prev &&
155 "target list head must have been initialized");
158 * insert the edge, if it is not yet in the set or return
159 * the instance in the set.
161 edge = set_insert(edges, templ, size, edge_hash(templ));
162 block_edge = (ir_block_edge_t *) edge;
165 assert(!edge->invalid && "Invalid edge encountered");
168 /* If the old target is not null, the edge is moved. */
171 list_move(&edge->list, head);
173 /* If the edge is a cf edge, move it from the successor list. */
175 list_move(&block_edge->succ_list, succ_head);
177 _get_irn_edge_info(old_tgt)->out_count -= 1;
180 /* The old target was null, thus, the edge is newly created. */
183 list_add(&edge->list, head);
186 * If the edge is cf edge, enter it into the successor list
187 * of the target node's block.
190 list_add(&block_edge->succ_list, succ_head);
193 _get_irn_edge_info(tgt)->out_count += 1;
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));
202 void edges_node_deleted(ir_node *old, ir_graph *irg)
204 if(edges_activated(irg)) {
205 int not_a_block = !is_Block(old);
210 DBG((dbg, LEVEL_5, "node deleted: %n\n", old));
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);
222 void edges_invalidate(ir_node *irn, ir_graph *irg)
224 edges_node_deleted(irn, irg);
227 static void build_edges_walker(ir_node *irn, void *data)
229 ir_graph *irg = data;
230 int not_a_block = !is_Block(irn);
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);
237 void edges_activate(ir_graph *irg)
239 irg_edge_info_t *info = _get_irg_edge_info(irg);
242 edges_init_graph(irg);
243 irg_walk_graph(irg, NULL, build_edges_walker, irg);
246 void edges_deactivate(ir_graph *irg)
248 irg_edge_info_t *info = _get_irg_edge_info(irg);
252 del_set(info->edges);
257 int (edges_activated)(const ir_graph *irg)
259 return _edges_activated(irg);
264 * Reroute all use-edges from a node to another.
265 * @param from The node whose use-edges shall be withdrawn.
266 * @param to The node to which all the use-edges of @p from shall be
269 void edges_reroute(ir_node *from, ir_node *to, ir_graph *irg)
271 if(edges_activated(irg)) {
272 struct list_head *head = _get_irn_outs_head(from);
274 DBG((firm_dbg_register(DBG_EDGES), LEVEL_5,
275 "reroute from %n to %n\n", from, to));
277 while(head != head->next) {
278 ir_edge_t *edge = list_entry(head->next, ir_edge_t, list);
279 // DBG((dbg, LEVEL_5, "\t%n %d\n", edge->src, edge->pos));
280 assert(edge->pos >= -1);
281 set_irn_n(edge->src, edge->pos, to);
286 static void verify_set_presence(ir_node *irn, void *data)
288 ir_graph *irg = data;
289 set *edges = _get_irg_edge_info(irg)->edges;
290 int not_a_block = !is_Block(irn);
293 for(i = 0, n = get_irn_arity(irn) + not_a_block; i < n; ++i) {
294 ir_block_edge_t space;
295 ir_edge_t *templ = (ir_edge_t *) &space;
297 size_t size = not_a_block ? sizeof(ir_edge_t) : sizeof(ir_block_edge_t);
300 templ->pos = i - not_a_block;
302 e = set_find(edges, templ, size, edge_hash(templ));
306 DBG((dbg, LEVEL_DEFAULT, "edge %n,%d is missing\n", irn, templ->pos));
310 static void verify_list_presence(ir_node *irn, void *data)
314 foreach_out_edge(irn, e) {
315 ir_node *tgt = get_irn_n(e->src, e->pos);
317 DBG((dbg, LEVEL_DEFAULT, "edge %n,%d is no out edge of %n but of %n\n",
318 e->src, e->pos, irn, tgt));
323 void edges_verify(ir_graph *irg)
325 set *edges = _get_irg_edge_info(irg)->edges;
328 /* Clear the present bit in all edges available. */
329 for(e = set_first(edges); e; e = set_next(edges))
332 irg_walk_graph(irg, verify_set_presence, verify_list_presence, irg);
335 * Dump all edges which are not invalid and not present.
336 * These edges are superfluous and their presence in the
339 for(e = set_first(edges); e; e = set_next(edges)) {
340 if(!e->invalid && !e->present)
341 DBG((dbg, LEVEL_DEFAULT, "edge %n,%d is superfluous\n", e->src, e->pos));
345 void init_edges(void)
347 dbg = firm_dbg_register(DBG_EDGES);
348 /* firm_dbg_set_mask(dbg, -1); */
352 const ir_edge_t *(get_irn_out_edge_first)(const ir_node *irn)
354 return _get_irn_out_edge_first(irn);
357 const ir_edge_t *(get_irn_out_edge_next)(const ir_node *irn, const ir_edge_t *last)
359 return _get_irn_out_edge_next(irn, last);
362 ir_node *(get_edge_src_irn)(const ir_edge_t *edge)
364 return _get_edge_src_irn(edge);
367 int (get_edge_src_pos)(const ir_edge_t *edge)
369 return _get_edge_src_pos(edge);
372 int (get_irn_n_edges)(const ir_node *irn)
374 return _get_irn_n_edges(irn);
378 #endif /* FIRM_EDGES_INPLACE */