2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Compute and access out edges (also called def-use edges).
23 * @author Goetz Lindenmaier, Michael Beck
33 #include "irgraph_t.h"
40 /*--------------------------------------------------------------------*/
41 /** Accessing the out datastructures **/
42 /*--------------------------------------------------------------------*/
45 /** Clear the outs of a node */
46 static void reset_outs(ir_node *node, void *unused)
54 int get_irn_outs_computed(const ir_node *node)
56 return node->out != NULL;
59 int get_irn_n_outs(const ir_node *node)
61 assert(node && node->kind == k_ir_node);
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;
69 ir_node *get_irn_out(const ir_node *def, int pos)
71 assert(pos >= 0 && pos < get_irn_n_outs(def));
73 assert(def->out_valid);
74 #endif /* defined DEBUG_libfirm */
75 return def->out[pos+1].use;
78 ir_node *get_irn_out_ex(const ir_node *def, int pos, int *in_pos)
80 assert(pos >= 0 && pos < get_irn_n_outs(def));
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;
88 void set_irn_out(ir_node *def, int pos, ir_node *use, int in_pos)
91 assert(pos >= 0 && pos < get_irn_n_outs(def));
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;
99 int get_Block_n_cfg_outs(const ir_node *bl)
101 int i, n_cfg_outs = 0;
102 assert(bl && is_Block(bl));
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;
114 int get_Block_n_cfg_outs_ka(const ir_node *bl)
116 int i, n_cfg_outs = 0;
117 assert(bl && is_Block(bl));
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) {
127 /* ignore End if we are in the Endblock */
128 if (get_nodes_block(succ) == bl)
130 else /* count Keep-alive as one */
133 n_cfg_outs += succ->out[0].pos;
139 ir_node *get_Block_cfg_out(const ir_node *bl, int pos)
142 assert(bl && is_Block(bl));
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;
151 return succ->out[pos + 1].use;
159 ir_node *get_Block_cfg_out_ka(const ir_node *bl, int pos)
162 assert(bl && is_Block(bl));
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) {
172 ir_node *end_bl = get_nodes_block(succ);
174 /* ignore End if we are in the Endblock */
178 /* handle keep-alive here: return the Endblock instead of the End node */
183 n_outs = succ->out[0].pos;
185 return succ->out[pos + 1].use;
194 static void irg_out_walk_2(ir_node *node, irg_walk_func *pre,
195 irg_walk_func *post, void *env)
201 assert(get_irn_visited(node) < get_irg_visited(current_ir_graph));
203 set_irn_visited(node, get_irg_visited(current_ir_graph));
205 if (pre) pre(node, env);
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);
213 if (post) post(node, env);
216 void irg_out_walk(ir_node *node, irg_walk_func *pre, irg_walk_func *post,
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);
227 static void irg_out_block_walk2(ir_node *bl, irg_walk_func *pre,
228 irg_walk_func *post, void *env)
232 if (!Block_block_visited(bl)) {
233 mark_Block_block_visited(bl);
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);
242 irg_out_block_walk2(pred, pre, post, env);
250 void irg_out_block_walk(ir_node *node, irg_walk_func *pre, irg_walk_func *post,
254 assert(is_Block(node) || (get_irn_mode(node) == mode_X));
256 inc_irg_block_visited(current_ir_graph);
258 if (get_irn_mode(node) == mode_X) {
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);
267 irg_out_block_walk2(node, pre, post, env);
271 /*--------------------------------------------------------------------*/
272 /** Building and Removing the out datastructure **/
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 /*--------------------------------------------------------------------*/
291 /** Returns the amount of out edges for not yet visited successors. */
292 static int _count_outs(ir_node *n)
294 int start, i, res, irn_arity;
297 n->out = (ir_def_use_edge*) INT_TO_PTR(1); /* Space for array size. */
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. */
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);
308 if (skipped_pred != pred) {
309 set_irn_n(n, i, skipped_pred);
312 /* count Def-Use edges for predecessors */
313 if (!irn_visited(skipped_pred))
314 res += _count_outs(skipped_pred);
316 /*count my Def-Use edges */
317 skipped_pred->out = (ir_def_use_edge*) INT_TO_PTR(PTR_TO_INT(skipped_pred->out) + 1);
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.
326 static int count_outs(ir_graph *irg)
331 inc_irg_visited(irg);
332 res = _count_outs(get_irg_end(irg));
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);
347 * Enter memory for the outs to a node.
349 * @param use current node
350 * @param free current free address in the chunk allocated for the outs
352 * @return The next free address
354 static ir_def_use_edge *_set_out_edges(ir_node *use, ir_def_use_edge *free)
356 int start, i, irn_arity, pos;
359 mark_irn_visited(use);
361 /* Allocate my array */
362 n_outs = PTR_TO_INT(use->out);
366 #endif /* defined DEBUG_libfirm */
368 /* We count the successors again, the space will be sufficient.
369 We use this counter to remember the position for the next back
373 start = is_Block(use) ? 0 : -1;
374 irn_arity = get_irn_arity(use);
376 for (i = start; i < irn_arity; ++i) {
377 ir_node *def = get_irn_n(use, i);
380 if (!irn_visited(def))
381 free = _set_out_edges(def, free);
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;
388 /* increase the number of Def-Use edges so far */
389 def->out[0].pos = pos;
395 * Enter memory for the outs to a node. Handles special nodes
397 * @param irg the graph
398 * @param free current free address in the chunk allocated for the outs
400 * @return The next free address
402 static ir_def_use_edge *set_out_edges(ir_graph *irg, ir_def_use_edge *free)
407 inc_irg_visited(irg);
408 free = _set_out_edges(get_irg_end(irg), free);
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);
418 #endif /* defined DEBUG_libfirm */
426 void compute_irg_outs(ir_graph *irg)
428 ir_graph *rem = current_ir_graph;
430 ir_def_use_edge *end = NULL; /* Only for debugging */
432 current_ir_graph = irg;
434 /* Update graph state */
435 assert(get_irg_phase_state(current_ir_graph) != phase_building);
437 free_irg_outs(current_ir_graph);
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);
443 /* allocate memory for all out edges. */
444 irg->outs = XMALLOCNZ(ir_def_use_edge, n_out_edges);
446 irg->n_outs = n_out_edges;
447 #endif /* defined DEBUG_libfirm */
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);
453 /* Check how much memory we have used */
454 assert (end == (irg->outs + n_out_edges));
456 set_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_OUTS);
457 current_ir_graph = rem;
460 void assure_irg_outs(ir_graph *irg)
462 if (! is_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_OUTS))
463 compute_irg_outs(irg);
466 void compute_irp_outs(void)
469 for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
470 compute_irg_outs(get_irp_irg(i));
473 void free_irp_outs(void)
476 for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
477 free_irg_outs(get_irp_irg(i));
480 void free_irg_outs(ir_graph *irg)
482 /* current_ir_graph->outs_state = outs_none; */
486 memset(irg->outs, 0, irg->n_outs);
487 #endif /* defined DEBUG_libfirm */
492 #endif /* defined 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 */