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