Fixed hook_strength_red()
[libfirm] / ir / ir / iredges.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/iredges.c
4  * Purpose:     Always available outs.
5  * Author:      Sebastian Hack
6  * Modified by: Michael Beck
7  * Created:     14.1.2005
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2006 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14  * Always available outs.
15  * @author Sebastian Hack
16  * @date 14.1.2005
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #ifdef HAVE_ALLOCA_H
24 #include <alloca.h>
25 #endif
26 #ifdef HAVE_MALLOC_H
27 #include <malloc.h>
28 #endif
29
30 #include "irnode_t.h"
31 #include "iropt_t.h"
32 #include "iredges_t.h"
33 #include "irgwalk.h"
34 #include "irdump_t.h"
35 #include "irprintf.h"
36 #include "irhooks.h"
37 #include "debug.h"
38 #include "set.h"
39
40 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
41
42 /**
43  * This flag is set to 1, if the edges get initialized for an irg.
44  * Then register additional data is forbidden.
45  */
46 static int edges_used = 0;
47
48 static int edges_private_size = 0;
49
50 int edges_register_private_data(size_t n)
51 {
52         int res = edges_private_size;
53
54         assert(!edges_used && "you cannot register private edge data, if edges have been initialized");
55
56         edges_private_size += n;
57         return res;
58 }
59
60 #define TIMES37(x) (((x) << 5) + ((x) << 2) + (x))
61
62 #define get_irn_out_list_head(irn) (&get_irn_out_info(irn)->outs)
63
64 static int edge_cmp(const void *p1, const void *p2, size_t len)
65 {
66         const ir_edge_t *e1 = p1;
67         const ir_edge_t *e2 = p2;
68         int res = e1->src == e2->src && e1->pos == e2->pos;
69
70         return !res;
71 }
72
73 #define edge_hash(edge) (TIMES37((edge)->pos) + HASH_PTR((edge)->src))
74
75 /**
76  * Initialize the out information for a graph.
77  * @note Dead node elimination can call this on an already initialized graph.
78  */
79 void edges_init_graph(ir_graph *irg)
80 {
81         if(edges_activated(irg)) {
82                 irg_edge_info_t *info = _get_irg_edge_info(irg);
83                 int amount = 2048;
84
85                 edges_used = 1;
86
87                 if(info->edges) {
88                         amount = set_count(info->edges);
89                         del_set(info->edges);
90                 }
91
92                 info->edges = new_set(edge_cmp, amount);
93         }
94 }
95
96 #define EDGE_SIZE(src) \
97     (edges_private_size + (is_Block(src) ? sizeof(ir_block_edge_t) : sizeof(ir_edge_t)))
98
99
100 /**
101  * Get the edge object of an outgoing edge at a node.
102  * @param   irg The graph, the node is in.
103  * @param   src The node at which the edge originates.
104  * @param   pos The position of the edge.
105  * @return      The corresponding edge object or NULL,
106  *              if no such edge exists.
107  */
108 const ir_edge_t *get_irn_edge(ir_graph *irg, const ir_node *src, int pos)
109 {
110         if(edges_activated(irg)) {
111                 irg_edge_info_t *info = _get_irg_edge_info(irg);
112                 size_t size           = EDGE_SIZE(src);
113                 ir_edge_t key;
114
115                 key.src = (ir_node *) src;
116                 key.pos = pos;
117                 return set_find(info->edges, &key, size, edge_hash(&key));
118         }
119
120         return NULL;
121 }
122
123 /**
124  * Change the out count
125  */
126 static INLINE void edge_change_cnt(ir_node *tgt, int ofs) {
127         irn_edge_info_t *info = _get_irn_edge_info(tgt);
128         info->out_count += ofs;
129 }
130
131 /* The edge from (src, pos) -> old_tgt is redirected to tgt */
132 void edges_notify_edge(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt, ir_graph *irg)
133 {
134         const char *msg = "";
135
136         if(!edges_activated(irg))
137                 return;
138
139 #if 0
140         assert(node_is_in_irgs_storage(irg, src) && "source not in irg");
141 #endif
142
143         /*
144          * Only do something, if the old and new target differ.
145          */
146         if(tgt != old_tgt) {
147                 int is_block_edge = is_Block(src);
148                 set *edges = _get_irg_edge_info(irg)->edges;
149                 ir_edge_t *edge;
150
151                 /*
152                  * This is scary, but:
153                  * If two entries in a set do not have the same size, they are
154                  * treated as unequal, ignoring the comparison function.
155                  * So, edges from blocks have extra storage (they are
156                  * ir_block_edge_t's).
157                  *
158                  * Also add the amount of registered private data to the
159                  * size of the edge.
160                  */
161                 size_t size      = EDGE_SIZE(src);
162                 ir_edge_t *templ = alloca(size);
163
164                 /* Initialize the edge template to search in the set. */
165                 memset(templ, 0, size);
166                 templ->src = src;
167                 templ->pos = pos;
168                 templ->invalid = 0;
169                 templ->present = 0;
170                 DEBUG_ONLY(templ->src_nr = get_irn_node_nr(src));
171
172                 /*
173                  * If the target is NULL, the edge shall be deleted.
174                  */
175                 if (tgt == NULL) {
176                         /* search the edge in the set. */
177                         edge = set_find(edges, templ, size, edge_hash(templ));
178
179                         /* mark the edge invalid if it was found */
180                         if(edge) {
181                                 ir_block_edge_t *block_edge = (ir_block_edge_t *) edge;
182
183                                 msg = "deleting";
184                                 list_del(&edge->list);
185                                 edge->invalid = 1;
186                                 edge->pos = -2;
187                                 edge->src = NULL;
188
189                                 /*
190                                  * If the edge is a cf edge, we delete it also
191                                  * from the list of all block successor edges.
192                                  */
193                                 if(is_block_edge) {
194                                         list_del(&block_edge->succ_list);
195                                         edge_change_cnt(old_tgt,  -1);
196                                 }
197                         }
198
199                         /* If the edge was not found issue a warning on the debug stream */
200                         else {
201                                 msg = "edge to delete not found!\n";
202                         }
203                 } /* if */
204
205                 /*
206                  * The target is not NULL and the old target differs
207                  * from the new target, the edge shall be moved (if the
208                  * old target was != NULL) or added (if the old target was
209                  * NULL).
210                  */
211                 else {
212                         struct list_head *head = _get_irn_outs_head(tgt);
213
214                         /*
215                          * The list head in the block of the edges target.
216                          * Therein all control flow edges directed at that block
217                          * are recorded.
218                          */
219                         struct list_head *succ_head =
220                                 is_block_edge ? _get_block_succ_head(get_nodes_block(tgt)) : NULL;
221
222                         ir_block_edge_t *block_edge;
223
224 #if 0
225                         if(!node_is_in_irgs_storage(irg, tgt))
226                                 return;
227 #endif
228                         assert(head->next && head->prev &&
229                                         "target list head must have been initialized");
230
231                         /*
232                          * insert the edge, if it is not yet in the set or return
233                          * the instance in the set.
234                          */
235                         edge = set_insert(edges, templ, size, edge_hash(templ));
236                         block_edge = (ir_block_edge_t *) edge;
237
238 #ifdef DEBUG_libfirm
239                         assert(!edge->invalid && "Invalid edge encountered");
240 #endif
241
242                         /* If the old target is not null, the edge is moved. */
243                         if(old_tgt) {
244                                 msg = "redirecting";
245
246                                 list_move(&edge->list, head);
247
248                                 /* If the edge is a cf edge, move it from the successor list. */
249                                 if(is_block_edge)
250                                         list_move(&block_edge->succ_list, succ_head);
251
252                                 edge_change_cnt(old_tgt,  -1);
253                         }
254
255                         /* The old target was null, thus, the edge is newly created. */
256                         else {
257                                 msg = "adding";
258                                 list_add(&edge->list, head);
259
260                                 /*
261                                  * If the edge is cf edge, enter it into the successor list
262                                  * of the target node's block.
263                                  */
264                                 if(is_block_edge)
265                                         list_add(&block_edge->succ_list, succ_head);
266                         }
267
268                         edge_change_cnt(tgt,  +1);
269                 } /* else */
270         }
271
272         /* If the target and the old target are equal, nothing is done. */
273         DBG((dbg, LEVEL_5, "announce out edge: %+F %d-> %+F(%+F): %s\n",
274                                 src, pos, tgt, old_tgt, msg));
275 }
276
277 void edges_node_deleted(ir_node *old, ir_graph *irg)
278 {
279         if(edges_activated(irg)) {
280                 int not_a_block = !is_Block(old);
281                 int i, n;
282
283                 DBG((dbg, LEVEL_5, "node deleted: %+F\n", old));
284
285                 /* Change to get_irn_n */
286                 for(i = -not_a_block, n = get_irn_arity(old); i < n; ++i) {
287                         ir_node *old_tgt = get_irn_n(old, i);
288                         DBG((dbg, LEVEL_5, "\tdelete to old target %+F\n", old_tgt));
289                         edges_notify_edge(old, i, NULL, old_tgt, irg);
290                 }
291
292         }
293 }
294
295 void edges_invalidate(ir_node *irn, ir_graph *irg) {
296         edges_node_deleted(irn, irg);
297 }
298
299 /**
300  * Post-Walker: notify all edges
301  */
302 static void build_edges_walker(ir_node *irn, void *data) {
303         ir_graph *irg = data;
304         int not_a_block = !is_Block(irn);
305         int i, n;
306
307         for (i = -not_a_block, n = get_irn_arity(irn); i < n; ++i)
308                 edges_notify_edge(irn, i, get_irn_n(irn, i), NULL, irg);
309 }
310
311 /**
312  * Pre-Walker: initializes the list-heads and set the out-count
313  * of all nodes to 0.
314  */
315 static void init_lh_walker(ir_node *irn, void *data) {
316         INIT_LIST_HEAD(_get_irn_outs_head(irn));
317         if (is_Block(irn))
318                 INIT_LIST_HEAD(_get_block_succ_head(irn));
319         _get_irn_edge_info(irn)->out_count = 0;
320 }
321
322 /**
323  * Visitor: initializes the list-heads and set the out-count
324  * of all nodes to 0 of nodes that are not seen so far.
325  */
326 static void visitor(ir_node *irn, void *data) {
327         if (irn_not_visited(irn)) {
328                 mark_irn_visited(irn);
329                 init_lh_walker(irn, data);
330         }
331 }
332
333 /*
334  * Build the initial edge set.
335  * Beware, this is not a simple task because it suffers from two
336  * difficulties:
337  * - the anchor set allows access to Nodes that may not be reachable from
338  *   the End node
339  * - the identities add nodes to the "root set" that are not yet reachable
340  *   from End. However, after some transformations, the CSE may revival these
341  *   nodes
342  *
343  * These problems can be fixed using different strategies:
344  * - Add an age flag to every node. Whenever the edge of a node is older
345  *   then the current edge, invalidate the edges of this node.
346  *   While this would help for revivaled nodes, it increases memory and runtime.
347  * - Delete the identities set.
348  *   Solves the revival problem, but may increase the memory consumption, as
349  *   nodes cannot be revivaled at all.
350  * - Manually iterate over the identities root set. This did not consume more memory
351  *   but increase the computation time because the |identies| >= |V|
352  *
353  * Currently, we use the last option.
354  */
355 void edges_activate(ir_graph *irg)
356 {
357         irg_edge_info_t *info = _get_irg_edge_info(irg);
358
359         info->activated = 1;
360         edges_init_graph(irg);
361         irg_walk_graph(irg, init_lh_walker, build_edges_walker, irg);
362         irg_walk_anchors(irg, init_lh_walker, NULL, irg);
363         visit_all_identities(irg, visitor, irg);
364 }
365
366 void edges_deactivate(ir_graph *irg)
367 {
368         irg_edge_info_t *info = _get_irg_edge_info(irg);
369
370         info->activated = 0;
371         if (info->edges) {
372                 del_set(info->edges);
373                 info->edges = NULL;
374         }
375 }
376
377 int (edges_activated)(const ir_graph *irg)
378 {
379         return _edges_activated(irg);
380 }
381
382
383 /**
384  * Reroute all use-edges from a node to another.
385  * @param from The node whose use-edges shall be withdrawn.
386  * @param to The node to which all the use-edges of @p from shall be
387  * sent to.
388  */
389 void edges_reroute(ir_node *from, ir_node *to, ir_graph *irg)
390 {
391         if(edges_activated(irg)) {
392                 struct list_head *head = _get_irn_outs_head(from);
393
394                 DBG((dbg, LEVEL_5,
395                                         "reroute from %+F to %+F\n", from, to));
396
397                 while(head != head->next) {
398                         ir_edge_t *edge = list_entry(head->next, ir_edge_t, list);
399                         // DBG((dbg, LEVEL_5, "\t%n %d\n", edge->src, edge->pos));
400                         assert(edge->pos >= -1);
401                         set_irn_n(edge->src, edge->pos, to);
402                 }
403         }
404 }
405
406 static void verify_set_presence(ir_node *irn, void *data)
407 {
408         ir_graph *irg = data;
409         set *edges = _get_irg_edge_info(irg)->edges;
410         int not_a_block = !is_Block(irn);
411         int i, n;
412
413         for(i = 0, n = get_irn_arity(irn) + not_a_block; i < n; ++i) {
414                 ir_block_edge_t space;
415                 ir_edge_t *templ = (ir_edge_t *) &space;
416                 ir_edge_t *e;
417                 size_t size = not_a_block ? sizeof(ir_edge_t) : sizeof(ir_block_edge_t);
418
419                 templ->src = irn;
420                 templ->pos = i - not_a_block;
421
422                 e = set_find(edges, templ, size, edge_hash(templ));
423                 if(e != NULL)
424                         e->present = 1;
425                 else
426                         DBG((dbg, LEVEL_DEFAULT, "edge %+F,%d is missing\n", irn, templ->pos));
427         }
428 }
429
430 static void verify_list_presence(ir_node *irn, void *data)
431 {
432         const ir_edge_t *e;
433
434         foreach_out_edge(irn, e) {
435                 ir_node *tgt = get_irn_n(e->src, e->pos);
436                 if(irn != tgt)
437                         DBG((dbg, LEVEL_DEFAULT, "edge %+F,%d is no out edge of %+F but of %+F\n",
438                                         e->src, e->pos, irn, tgt));
439         }
440
441 }
442
443 void edges_verify(ir_graph *irg)
444 {
445         set *edges = _get_irg_edge_info(irg)->edges;
446         ir_edge_t *e;
447
448         /* Clear the present bit in all edges available. */
449         for(e = set_first(edges); e; e = set_next(edges))
450                 e->present = 0;
451
452         irg_walk_graph(irg, verify_set_presence, verify_list_presence, irg);
453
454         /*
455          * Dump all edges which are not invalid and not present.
456          * These edges are superfluous and their presence in the
457          * edge set is wrong.
458          */
459         for(e = set_first(edges); e; e = set_next(edges)) {
460                 if(!e->invalid && !e->present)
461                         DBG((dbg, LEVEL_DEFAULT, "edge %+F,%d is superfluous\n", e->src, e->pos));
462         }
463 }
464
465 void init_edges(void)
466 {
467         FIRM_DBG_REGISTER(dbg, DBG_EDGES);
468         /* firm_dbg_set_mask(dbg, -1); */
469 }
470
471
472 const ir_edge_t *(get_irn_out_edge_first)(const ir_node *irn)
473 {
474         return _get_irn_out_edge_first(irn);
475 }
476
477 const ir_edge_t *(get_irn_out_edge_next)(const ir_node *irn, const ir_edge_t *last)
478 {
479         return _get_irn_out_edge_next(irn, last);
480 }
481
482 ir_node *(get_edge_src_irn)(const ir_edge_t *edge)
483 {
484         return _get_edge_src_irn(edge);
485 }
486
487 int (get_edge_src_pos)(const ir_edge_t *edge)
488 {
489         return _get_edge_src_pos(edge);
490 }
491
492 int (get_irn_n_edges)(const ir_node *irn)
493 {
494         return _get_irn_n_edges(irn);
495 }