2 * Always available outs.
3 * @author Sebastian Hack
15 static firm_dbg_module_t *dbg;
17 #define TIMES37(x) (((x) << 5) + ((x) << 2) + (x))
19 #define get_irn_out_list_head(irn) (&get_irn_out_info(irn)->outs)
21 static int edge_cmp(const void *p1, const void *p2, size_t len)
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;
30 static INLINE unsigned edge_hash(const ir_edge_t *edge)
32 unsigned result = HASH_PTR(edge->src);
33 result = TIMES37(result) + edge->pos;
38 * Initialize the out information for a graph.
39 * @note Dead node elim can call this on an already initialized graph.
41 void edges_init_graph(ir_graph *irg)
43 if(edges_activated(irg)) {
44 irg_edge_info_t *info = _get_irg_edge_info(irg);
48 amount = set_count(info->edges);
52 info->edges = new_set(edge_cmp, amount);
56 void edges_notify_edge(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt, ir_graph *irg)
60 if(!edges_activated(irg))
63 assert(node_is_in_irgs_storage(irg, src) && "source not in irg");
66 * Only do something, if the old and new target differ.
69 set *edges = _get_irg_edge_info(irg)->edges;
73 /* Initialize the edge template to search in the set. */
75 templ.src_nr = get_irn_node_nr(src);
81 INIT_LIST_HEAD(&templ.list);
84 * If the target is NULL, the edge shall be deleted.
87 /* search the edge in the set. */
88 edge = set_find(edges, &templ, sizeof(templ), edge_hash(&templ));
90 /* mark the edge invalid if it was found */
93 list_del(&edge->list);
99 /* If the edge was not found issue a warning on the debug stream */
101 msg = "edge to delete not found!\n";
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
112 struct list_head *head = _get_irn_outs_head(tgt);
114 if(!node_is_in_irgs_storage(irg, tgt))
117 assert(head->next && head->prev &&
118 "target list head must have been initialized");
121 * insert the edge, if it is not yet in the set or return
122 * the instance in the set.
124 edge = set_insert(edges, &templ, sizeof(templ), edge_hash(&templ));
127 assert(!edge->invalid && "Invalid edge encountered");
130 /* If the old target is not null, the edge is moved. */
133 list_move(&edge->list, head);
134 _get_irn_edge_info(old_tgt)->out_count -= 1;
137 /* The old target was null, thus, the edge is newly created. */
140 list_add(&edge->list, head);
143 _get_irn_edge_info(tgt)->out_count += 1;
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));
152 void edges_node_deleted(ir_node *old, ir_graph *irg)
154 if(edges_activated(irg)) {
155 int not_a_block = !is_Block(old);
160 DBG((dbg, LEVEL_5, "node deleted: %n\n", old));
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);
172 void edges_invalidate(ir_node *irn, ir_graph *irg)
174 edges_node_deleted(irn, irg);
178 static void build_edges_walker(ir_node *irn, void *data)
180 ir_graph *irg = data;
181 int not_a_block = !is_Block(irn);
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);
188 void edges_activate(ir_graph *irg)
190 irg_edge_info_t *info = _get_irg_edge_info(irg);
193 edges_init_graph(irg);
194 irg_walk_graph(irg, NULL, build_edges_walker, irg);
197 void edges_deactivate(ir_graph *irg)
199 irg_edge_info_t *info = _get_irg_edge_info(irg);
203 del_set(info->edges);
206 int (edges_activated)(const ir_graph *irg)
208 return _edges_activated(irg);
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
218 void edges_reroute(ir_node *from, ir_node *to, ir_graph *irg)
220 if(edges_activated(irg)) {
221 struct list_head *head = _get_irn_outs_head(from);
223 DBG((firm_dbg_register(DBG_EDGES), LEVEL_5,
224 "reroute from %n to %n\n", from, to));
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);
235 static void verify_set_presence(ir_node *irn, void *data)
237 ir_graph *irg = data;
238 set *edges = _get_irg_edge_info(irg)->edges;
239 int not_a_block = !is_Block(irn);
242 for(i = 0, n = get_irn_arity(irn) + not_a_block; i < n; ++i) {
247 templ.pos = i - not_a_block;
249 e = set_find(edges, &templ, sizeof(templ), edge_hash(&templ));
253 ir_fprintf(stderr, "edge %n,%d is missing\n", irn, templ.pos);
257 static void verify_list_presence(ir_node *irn, void *data)
261 foreach_out_edge(irn, e) {
262 ir_node *tgt = get_irn_n(e->src, e->pos);
264 ir_fprintf(stderr, "edge %n,%d is no out edge of %n but of %n\n",
265 e->src, e->pos, irn, tgt);
270 void edges_verify(ir_graph *irg)
272 set *edges = _get_irg_edge_info(irg)->edges;
275 /* Clear the present bit in all edges available. */
276 for(e = set_first(edges); e; e = set_next(edges))
279 irg_walk_graph(irg, verify_set_presence, verify_list_presence, irg);
282 * Dump all edges which are not invalid and not present.
283 * These edges are superfluous and their presence in the
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);
292 void init_edges(void)
294 dbg = firm_dbg_register(DBG_EDGES);
295 /* firm_dbg_set_mask(dbg, -1); */
299 const ir_edge_t *(get_irn_out_edge_first)(const ir_node *irn)
301 return _get_irn_out_edge_first(irn);
304 const ir_edge_t *(get_irn_out_edge_next)(const ir_node *irn, const ir_edge_t *last)
306 return _get_irn_out_edge_next(irn, last);
309 ir_node *(get_edge_src_irn)(const ir_edge_t *edge)
311 return _get_edge_src_irn(edge);
314 int (get_edge_src_pos)(const ir_edge_t *edge)
316 return _get_edge_src_pos(edge);