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