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