2 * Always available outs.
3 * @author Sebastian Hack
11 #include "iredges_t.h"
18 static firm_dbg_module_t *dbg;
20 #if FIRM_EDGES_INPLACE
22 #define TIMES37(x) (((x) << 5) + ((x) << 2) + (x))
24 #define get_irn_out_list_head(irn) (&get_irn_out_info(irn)->outs)
26 static int edge_cmp(const void *p1, const void *p2, size_t len)
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;
35 static INLINE unsigned edge_hash(const ir_edge_t *edge)
37 unsigned result = HASH_PTR(edge->src);
38 result = TIMES37(result) + edge->pos;
43 * Initialize the out information for a graph.
44 * @note Dead node elim can call this on an already initialized graph.
46 void edges_init_graph(ir_graph *irg)
48 if(edges_activated(irg)) {
49 irg_edge_info_t *info = _get_irg_edge_info(irg);
53 amount = set_count(info->edges);
57 info->edges = new_set(edge_cmp, amount);
61 void edges_notify_edge(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt, ir_graph *irg)
65 if(!edges_activated(irg))
68 assert(node_is_in_irgs_storage(irg, src) && "source not in irg");
71 * Only do something, if the old and new target differ.
74 set *edges = _get_irg_edge_info(irg)->edges;
78 /* Initialize the edge template to search in the set. */
80 templ.src_nr = get_irn_node_nr(src);
86 INIT_LIST_HEAD(&templ.list);
89 * If the target is NULL, the edge shall be deleted.
92 /* search the edge in the set. */
93 edge = set_find(edges, &templ, sizeof(templ), edge_hash(&templ));
95 /* mark the edge invalid if it was found */
98 list_del(&edge->list);
104 /* If the edge was not found issue a warning on the debug stream */
106 msg = "edge to delete not found!\n";
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
117 struct list_head *head = _get_irn_outs_head(tgt);
119 if(!node_is_in_irgs_storage(irg, tgt))
122 assert(head->next && head->prev &&
123 "target list head must have been initialized");
126 * insert the edge, if it is not yet in the set or return
127 * the instance in the set.
129 edge = set_insert(edges, &templ, sizeof(templ), edge_hash(&templ));
132 assert(!edge->invalid && "Invalid edge encountered");
135 /* If the old target is not null, the edge is moved. */
138 list_move(&edge->list, head);
139 _get_irn_edge_info(old_tgt)->out_count -= 1;
142 /* The old target was null, thus, the edge is newly created. */
145 list_add(&edge->list, head);
148 _get_irn_edge_info(tgt)->out_count += 1;
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));
157 void edges_node_deleted(ir_node *old, ir_graph *irg)
159 if(edges_activated(irg)) {
160 int not_a_block = !is_Block(old);
165 DBG((dbg, LEVEL_5, "node deleted: %n\n", old));
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);
177 void edges_invalidate(ir_node *irn, ir_graph *irg)
179 edges_node_deleted(irn, irg);
183 static void build_edges_walker(ir_node *irn, void *data)
185 ir_graph *irg = data;
186 int not_a_block = !is_Block(irn);
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);
193 void edges_activate(ir_graph *irg)
195 irg_edge_info_t *info = _get_irg_edge_info(irg);
198 edges_init_graph(irg);
199 irg_walk_graph(irg, NULL, build_edges_walker, irg);
202 void edges_deactivate(ir_graph *irg)
204 irg_edge_info_t *info = _get_irg_edge_info(irg);
208 del_set(info->edges);
211 int (edges_activated)(const ir_graph *irg)
213 return _edges_activated(irg);
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
223 void edges_reroute(ir_node *from, ir_node *to, ir_graph *irg)
225 if(edges_activated(irg)) {
226 struct list_head *head = _get_irn_outs_head(from);
228 DBG((firm_dbg_register(DBG_EDGES), LEVEL_5,
229 "reroute from %n to %n\n", from, to));
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);
240 static void verify_set_presence(ir_node *irn, void *data)
242 ir_graph *irg = data;
243 set *edges = _get_irg_edge_info(irg)->edges;
244 int not_a_block = !is_Block(irn);
247 for(i = 0, n = get_irn_arity(irn) + not_a_block; i < n; ++i) {
252 templ.pos = i - not_a_block;
254 e = set_find(edges, &templ, sizeof(templ), edge_hash(&templ));
258 DBG((dbg, LEVEL_DEFAULT, "edge %n,%d is missing\n", irn, templ.pos));
262 static void verify_list_presence(ir_node *irn, void *data)
266 foreach_out_edge(irn, e) {
267 ir_node *tgt = get_irn_n(e->src, e->pos);
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));
275 void edges_verify(ir_graph *irg)
277 set *edges = _get_irg_edge_info(irg)->edges;
280 /* Clear the present bit in all edges available. */
281 for(e = set_first(edges); e; e = set_next(edges))
284 irg_walk_graph(irg, verify_set_presence, verify_list_presence, irg);
287 * Dump all edges which are not invalid and not present.
288 * These edges are superfluous and their presence in the
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));
297 void init_edges(void)
299 dbg = firm_dbg_register(DBG_EDGES);
300 /* firm_dbg_set_mask(dbg, -1); */
304 const ir_edge_t *(get_irn_out_edge_first)(const ir_node *irn)
306 return _get_irn_out_edge_first(irn);
309 const ir_edge_t *(get_irn_out_edge_next)(const ir_node *irn, const ir_edge_t *last)
311 return _get_irn_out_edge_next(irn, last);
314 ir_node *(get_edge_src_irn)(const ir_edge_t *edge)
316 return _get_edge_src_irn(edge);
319 int (get_edge_src_pos)(const ir_edge_t *edge)
321 return _get_edge_src_pos(edge);
324 #endif /* FIRM_EDGES_INPLACE */