consolidate utility macros in util.h
[libfirm] / ir / ana / irouts.c
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    Compute and access out edges (also called def-use edges).
23  * @author   Goetz Lindenmaier, Michael Beck
24  * @date     1.2002
25  * @version  $Id$
26  */
27 #include "config.h"
28
29 #include <string.h>
30
31 #include "xmalloc.h"
32 #include "irouts.h"
33 #include "irnode_t.h"
34 #include "irgraph_t.h"
35 #include "irprog_t.h"
36 #include "irgwalk.h"
37 #include "util.h"
38 #include "irprintf.h"
39 #include "error.h"
40
41 #ifdef DEBUG_libfirm
42 /* Note:  ir_node.out_valid and ir_graph.n_outs are only present when DEBUG_libfirm is defined */
43 /* Accesses to out_valid and n_outs are fenced out to avoid breakage
44    when compiling with neither DEBUG_libfirm or NDEBUG defined */
45 #endif /* defined DEBUG_libfirm */
46
47 /*--------------------------------------------------------------------*/
48 /** Accessing the out datastructures                                 **/
49 /*--------------------------------------------------------------------*/
50
51 #ifdef DEBUG_libfirm
52 /** Clear the outs of a node */
53 static void reset_outs(ir_node *node, void *unused)
54 {
55         (void) unused;
56         node->out       = NULL;
57         node->out_valid = 0;
58 }
59 #endif
60
61 int get_irn_outs_computed(const ir_node *node)
62 {
63         return node->out != NULL;
64 }
65
66 /* returns the number of successors of the node: */
67 int get_irn_n_outs(const ir_node *node)
68 {
69         assert(node && node->kind == k_ir_node);
70 #ifdef DEBUG_libfirm
71         assert(node->out_valid);
72 #endif /* defined DEBUG_libfirm */
73         /* we misuse the first for the size info of the out array */
74         return node->out[0].pos;
75 }
76
77 /* Access successor n */
78 ir_node *get_irn_out(const ir_node *def, int pos)
79 {
80         assert(pos >= 0 && pos < get_irn_n_outs(def));
81 #ifdef DEBUG_libfirm
82         assert(def->out_valid);
83 #endif /* defined DEBUG_libfirm */
84         return def->out[pos+1].use;
85 }
86
87 /* Access successor n */
88 ir_node *get_irn_out_ex(const ir_node *def, int pos, int *in_pos)
89 {
90         assert(pos >= 0 && pos < get_irn_n_outs(def));
91 #ifdef DEBUG_libfirm
92         assert(def->out_valid);
93 #endif /* defined DEBUG_libfirm */
94         *in_pos = def->out[pos+1].pos;
95         return def->out[pos+1].use;
96 }
97
98 void set_irn_out(ir_node *def, int pos, ir_node *use, int in_pos)
99 {
100         assert(def && use);
101         assert(pos >= 0 && pos < get_irn_n_outs(def));
102 #ifdef DEBUG_libfirm
103         assert(def->out_valid);
104 #endif /* defined DEBUG_libfirm */
105         def->out[pos+1].use = use;
106         def->out[pos+1].pos = in_pos;
107 }
108
109 /* Return the number of control flow successors, ignore keep-alives. */
110 int get_Block_n_cfg_outs(const ir_node *bl)
111 {
112         int i, n_cfg_outs = 0;
113         assert(bl && is_Block(bl));
114 #ifdef DEBUG_libfirm
115         assert(bl->out_valid);
116 #endif /* defined DEBUG_libfirm */
117         for (i = 1; i <= bl->out[0].pos; ++i) {
118                 ir_node *succ = bl->out[i].use;
119                 if (get_irn_mode(succ) == mode_X && !is_End(succ) && !is_Bad(succ))
120                         n_cfg_outs += succ->out[0].pos;
121         }
122         return n_cfg_outs;
123 }
124
125 /* Return the number of control flow successors, honor keep-alives. */
126 int get_Block_n_cfg_outs_ka(const ir_node *bl)
127 {
128         int i, n_cfg_outs = 0;
129         assert(bl && is_Block(bl));
130 #ifdef DEBUG_libfirm
131         assert(bl->out_valid);
132 #endif /* defined DEBUG_libfirm */
133         for (i = 1; i <= bl->out[0].pos; ++i) {
134                 ir_node *succ = bl->out[i].use;
135                 if (get_irn_mode(succ) == mode_X) {
136                         if (is_Bad(succ))
137                                 continue;
138                         if (is_End(succ)) {
139                                 /* ignore End if we are in the Endblock */
140                                 if (get_nodes_block(succ) == bl)
141                                         continue;
142                                 else /* count Keep-alive as one */
143                                         n_cfg_outs += 1;
144                         } else
145                                 n_cfg_outs += succ->out[0].pos;
146                 }
147         }
148         return n_cfg_outs;
149 }
150
151 /* Access predecessor n, ignore keep-alives. */
152 ir_node *get_Block_cfg_out(const ir_node *bl, int pos)
153 {
154         int i;
155         assert(bl && is_Block(bl));
156 #ifdef DEBUG_libfirm
157         assert(bl->out_valid);
158 #endif /* defined DEBUG_libfirm */
159         for (i = 1; i <= bl->out[0].pos; ++i) {
160                 ir_node *succ = bl->out[i].use;
161                 if (get_irn_mode(succ) == mode_X && !is_End(succ) && !is_Bad(succ)) {
162                         int n_outs = succ->out[0].pos;
163                         if (pos < n_outs)
164                                 return succ->out[pos + 1].use;
165                         else
166                                 pos -= n_outs;
167                 }
168         }
169         return NULL;
170 }
171
172 /* Access predecessor n, honor keep-alives. */
173 ir_node *get_Block_cfg_out_ka(const ir_node *bl, int pos)
174 {
175         int i, n_outs;
176         assert(bl && is_Block(bl));
177 #ifdef DEBUG_libfirm
178         assert (bl->out_valid);
179 #endif /* defined DEBUG_libfirm */
180         for (i = 1; i <= bl->out[0].pos; ++i) {
181                 ir_node *succ = bl->out[i].use;
182                 if (get_irn_mode(succ) == mode_X) {
183                         if (is_Bad(succ))
184                                 continue;
185                         if (is_End(succ)) {
186                                 ir_node *end_bl = get_nodes_block(succ);
187                                 if (end_bl == bl) {
188                                         /* ignore End if we are in the Endblock */
189                                         continue;
190                                 }
191                                 if (pos == 0) {
192                                         /* handle keep-alive here: return the Endblock instead of the End node */
193                                         return end_bl;
194                                 } else
195                                         --pos;
196                         } else {
197                                 n_outs = succ->out[0].pos;
198                                 if (pos < n_outs)
199                                         return succ->out[pos + 1].use;
200                                 else
201                                         pos -= n_outs;
202                         }
203                 }
204         }
205         return NULL;
206 }
207
208 static void irg_out_walk_2(ir_node *node, irg_walk_func *pre,
209                            irg_walk_func *post, void *env)
210 {
211         int     i, n;
212         ir_node *succ;
213
214         assert(node);
215         assert(get_irn_visited(node) < get_irg_visited(current_ir_graph));
216
217         set_irn_visited(node, get_irg_visited(current_ir_graph));
218
219         if (pre) pre(node, env);
220
221         for (i = 0, n = get_irn_n_outs(node); i < n; ++i) {
222                 succ = get_irn_out(node, i);
223                 if (get_irn_visited(succ) < get_irg_visited(current_ir_graph))
224                         irg_out_walk_2(succ, pre, post, env);
225         }
226
227         if (post) post(node, env);
228 }
229
230 void irg_out_walk(ir_node *node, irg_walk_func *pre, irg_walk_func *post,
231                   void *env)
232 {
233         assert(node);
234         ir_graph *irg = get_irn_irg(node);
235         if (is_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_OUTS)) {
236                 inc_irg_visited (irg);
237                 irg_out_walk_2(node, pre, post, env);
238         }
239 }
240
241 static void irg_out_block_walk2(ir_node *bl, irg_walk_func *pre,
242                                 irg_walk_func *post, void *env)
243 {
244         int i, n;
245
246         if (!Block_block_visited(bl)) {
247                 mark_Block_block_visited(bl);
248
249                 if (pre)
250                         pre(bl, env);
251
252                 for (i = 0, n =  get_Block_n_cfg_outs(bl); i < n; ++i) {
253                         /* find the corresponding predecessor block. */
254                         ir_node *pred = get_Block_cfg_out(bl, i);
255                         /* recursion */
256                         irg_out_block_walk2(pred, pre, post, env);
257                 }
258
259                 if (post)
260                         post(bl, env);
261         }
262 }
263
264 /* Walks only over Block nodes in the graph.  Has its own visited
265    flag, so that it can be interleaved with the other walker.         */
266 void irg_out_block_walk(ir_node *node, irg_walk_func *pre, irg_walk_func *post,
267                         void *env)
268 {
269
270         assert(is_Block(node) || (get_irn_mode(node) == mode_X));
271
272         inc_irg_block_visited(current_ir_graph);
273
274         if (get_irn_mode(node) == mode_X) {
275                 int i, n;
276
277                 for (i = 0, n = get_irn_n_outs(node); i < n; ++i) {
278                         ir_node *succ = get_irn_out(node, i);
279                         irg_out_block_walk2(succ, pre, post, env);
280                 }
281         }
282         else {
283                 irg_out_block_walk2(node, pre, post, env);
284         }
285 }
286
287 /*--------------------------------------------------------------------*/
288 /** Building and Removing the out datastructure                      **/
289 /**                                                                  **/
290 /** The outs of a graph are allocated in a single, large array.      **/
291 /** This allows to allocate and deallocate the memory for the outs   **/
292 /** on demand.  The large array is separated into many small ones    **/
293 /** for each node.  Only a single field to reference the out array   **/
294 /** is stored in each node and a field referencing the large out     **/
295 /** array in irgraph.  The 0 field of each out array contains the    **/
296 /** size of this array.  This saves memory in the irnodes themselves.**/
297 /** The construction does two passes over the graph.  The first pass **/
298 /** counts the overall number of outs and the outs of each node.  It **/
299 /** stores the outs of each node in the out reference of the node.   **/
300 /** Then the large array is allocated.  The second iteration chops   **/
301 /** the large array into smaller parts, sets the out edges and       **/
302 /** recounts the out edges.                                          **/
303 /** Removes Tuple nodes!                                             **/
304 /*--------------------------------------------------------------------*/
305
306
307 /** Returns the amount of out edges for not yet visited successors. */
308 static int _count_outs(ir_node *n)
309 {
310         int start, i, res, irn_arity;
311
312         mark_irn_visited(n);
313         n->out = (ir_def_use_edge*) INT_TO_PTR(1);     /* Space for array size. */
314
315         start = is_Block(n) ? 0 : -1;
316         irn_arity = get_irn_arity(n);
317         res = irn_arity - start + 1;  /* --1 or --0; 1 for array size. */
318
319         for (i = start; i < irn_arity; ++i) {
320                 /* Optimize Tuples.  They annoy if walking the cfg. */
321                 ir_node *pred         = get_irn_n(n, i);
322                 ir_node *skipped_pred = skip_Tuple(pred);
323
324                 if (skipped_pred != pred) {
325                         set_irn_n(n, i, skipped_pred);
326                 }
327
328                 /* count Def-Use edges for predecessors */
329                 if (!irn_visited(skipped_pred))
330                         res += _count_outs(skipped_pred);
331
332                 /*count my Def-Use edges */
333                 skipped_pred->out = (ir_def_use_edge*) INT_TO_PTR(PTR_TO_INT(skipped_pred->out) + 1);
334         }
335         return res;
336 }
337
338
339 /** Returns the amount of out edges for not yet visited successors.
340  *  This version handles some special nodes like irg_frame, irg_args etc.
341  */
342 static int count_outs(ir_graph *irg)
343 {
344         ir_node *n;
345         int     i, res;
346
347         inc_irg_visited(irg);
348         res = _count_outs(get_irg_end(irg));
349
350         /* Now handle anchored nodes. We need the out count of those
351            even if they are not visible. */
352         for (i = anchor_last - 1; i >= 0; --i) {
353                 n = get_irg_anchor(irg, i);
354                 if (!irn_visited_else_mark(n)) {
355                         n->out = (ir_def_use_edge*) INT_TO_PTR(1);
356                         ++res;
357                 }
358         }
359         return res;
360 }
361
362 /**
363  * Enter memory for the outs to a node.
364  *
365  * @param use    current node
366  * @param free   current free address in the chunk allocated for the outs
367  *
368  * @return The next free address
369  */
370 static ir_def_use_edge *_set_out_edges(ir_node *use, ir_def_use_edge *free)
371 {
372         int    start, i, irn_arity, pos;
373         size_t n_outs;
374
375         mark_irn_visited(use);
376
377         /* Allocate my array */
378         n_outs = PTR_TO_INT(use->out);
379         use->out = free;
380 #ifdef DEBUG_libfirm
381         use->out_valid = 1;
382 #endif /* defined DEBUG_libfirm */
383         free += n_outs;
384         /* We count the successors again, the space will be sufficient.
385            We use this counter to remember the position for the next back
386            edge. */
387         use->out[0].pos = 0;
388
389         start = is_Block(use) ? 0 : -1;
390         irn_arity = get_irn_arity(use);
391
392         for (i = start; i < irn_arity; ++i) {
393                 ir_node *def = get_irn_n(use, i);
394
395                 /* Recursion */
396                 if (!irn_visited(def))
397                         free = _set_out_edges(def, free);
398
399                 /* Remember this Def-Use edge */
400                 pos = def->out[0].pos + 1;
401                 def->out[pos].use = use;
402                 def->out[pos].pos = i;
403
404                 /* increase the number of Def-Use edges so far */
405                 def->out[0].pos = pos;
406         }
407         return free;
408 }
409
410 /**
411  * Enter memory for the outs to a node. Handles special nodes
412  *
413  * @param irg    the graph
414  * @param free   current free address in the chunk allocated for the outs
415  *
416  * @return The next free address
417  */
418 static ir_def_use_edge *set_out_edges(ir_graph *irg, ir_def_use_edge *free)
419 {
420         ir_node *n;
421         int     i;
422
423         inc_irg_visited(irg);
424         free = _set_out_edges(get_irg_end(irg), free);
425
426         /* handle anchored nodes */
427         for (i = anchor_last - 1; i >= 0; --i) {
428                 n = get_irg_anchor(irg, i);
429                 if (!irn_visited_else_mark(n)) {
430                         size_t n_outs = PTR_TO_INT(n->out);
431                         n->out = free;
432 #ifdef DEBUG_libfirm
433                         n->out_valid = 1;
434 #endif /* defined DEBUG_libfirm */
435                         free += n_outs;
436                 }
437         }
438
439         return free;
440 }
441
442 /* compute the outs for a given graph */
443 void compute_irg_outs(ir_graph *irg)
444 {
445         ir_graph        *rem = current_ir_graph;
446         int             n_out_edges = 0;
447         ir_def_use_edge *end = NULL;         /* Only for debugging */
448
449         current_ir_graph = irg;
450
451         /* Update graph state */
452         assert(get_irg_phase_state(current_ir_graph) != phase_building);
453
454         free_irg_outs(current_ir_graph);
455
456         /* This first iteration counts the overall number of out edges and the
457            number of out edges for each node. */
458         n_out_edges = count_outs(irg);
459
460         /* allocate memory for all out edges. */
461         irg->outs = XMALLOCNZ(ir_def_use_edge, n_out_edges);
462 #ifdef DEBUG_libfirm
463         irg->n_outs = n_out_edges;
464 #endif /* defined DEBUG_libfirm */
465
466         /* The second iteration splits the irg->outs array into smaller arrays
467            for each node and writes the back edges into this array. */
468         end = set_out_edges(irg, irg->outs);
469
470         /* Check how much memory we have used */
471         assert (end == (irg->outs + n_out_edges));
472
473         set_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_OUTS);
474         current_ir_graph = rem;
475 }
476
477 void assure_irg_outs(ir_graph *irg)
478 {
479         if (! is_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_OUTS))
480                 compute_irg_outs(irg);
481 }
482
483 void compute_irp_outs(void)
484 {
485         size_t i, n;
486         for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
487                 compute_irg_outs(get_irp_irg(i));
488 }
489
490 void free_irp_outs(void)
491 {
492         size_t i, n;
493         for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
494                 free_irg_outs(get_irp_irg(i));
495 }
496
497 void free_irg_outs(ir_graph *irg)
498 {
499         /*   current_ir_graph->outs_state = outs_none; */
500
501         if (irg->outs) {
502 #ifdef DEBUG_libfirm
503                 memset(irg->outs, 0, irg->n_outs);
504 #endif /* defined DEBUG_libfirm */
505                 free(irg->outs);
506                 irg->outs = NULL;
507 #ifdef DEBUG_libfirm
508                 irg->n_outs = 0;
509 #endif /* defined DEBUG_libfirm */
510         }
511
512 #ifdef DEBUG_libfirm
513         /* when debugging, *always* reset all nodes' outs!  irg->outs might
514            have been lying to us */
515         irg_walk_graph (irg, reset_outs, NULL, NULL);
516 #endif /* defined DEBUG_libfirm */
517 }