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