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