2 * Copyright (C) 1995-2008 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
34 #include "irgraph_t.h"
42 /* Note: ir_node.out_valid and ir_graph.n_outs are only present when DEBUG_libfirm is defined */
43 /* Accesses to out_valid and n_outs are fenced out to avoid breakage
44 when compiling with neither DEBUG_libfirm or NDEBUG defined */
45 #endif /* defined DEBUG_libfirm */
47 /*--------------------------------------------------------------------*/
48 /** Accessing the out datastructures **/
49 /*--------------------------------------------------------------------*/
52 /** Clear the outs of a node */
53 static void reset_outs(ir_node *node, void *unused)
61 int get_irn_outs_computed(const ir_node *node)
63 return node->out != NULL;
66 /* returns the number of successors of the node: */
67 int get_irn_n_outs(const ir_node *node)
69 assert(node && node->kind == k_ir_node);
71 /* assert(node->out_valid); */
72 #endif /* defined DEBUG_libfirm */
73 /* we misuse the first for the size info of the out array */
74 return node->out[0].pos;
77 /* Access successor n */
78 ir_node *get_irn_out(const ir_node *def, int pos)
80 assert(pos >= 0 && pos < get_irn_n_outs(def));
82 /* assert(def->out_valid); */
83 #endif /* defined DEBUG_libfirm */
84 return def->out[pos+1].use;
87 /* Access successor n */
88 ir_node *get_irn_out_ex(const ir_node *def, int pos, int *in_pos)
90 assert(pos >= 0 && pos < get_irn_n_outs(def));
92 /* assert(def->out_valid); */
93 #endif /* defined DEBUG_libfirm */
94 *in_pos = def->out[pos+1].pos;
95 return def->out[pos+1].use;
98 void set_irn_out(ir_node *def, int pos, ir_node *use, int in_pos)
101 assert(pos >= 0 && pos < get_irn_n_outs(def));
103 def->out_valid = 1; /* assume that this function is used correctly */
104 #endif /* defined DEBUG_libfirm */
105 def->out[pos+1].use = use;
106 def->out[pos+1].pos = in_pos;
109 /* Return the number of control flow successors, ignore keep-alives. */
110 int get_Block_n_cfg_outs(const ir_node *bl)
112 int i, n_cfg_outs = 0;
113 assert(bl && is_Block(bl));
115 assert(bl->out_valid);
116 #endif /* defined DEBUG_libfirm */
117 for (i = 1; i <= bl->out[0].pos; ++i) {
118 ir_node *succ = bl->out[i].use;
119 if (get_irn_mode(succ) == mode_X && !is_End(succ))
120 n_cfg_outs += succ->out[0].pos;
125 /* Return the number of control flow successors, honor keep-alives. */
126 int get_Block_n_cfg_outs_ka(const ir_node *bl)
128 int i, n_cfg_outs = 0;
129 assert(bl && is_Block(bl));
131 assert(bl->out_valid);
132 #endif /* defined DEBUG_libfirm */
133 for (i = 1; i <= bl->out[0].pos; ++i) {
134 ir_node *succ = bl->out[i].use;
135 if (get_irn_mode(succ) == mode_X) {
138 /* ignore End if we are in the Endblock */
139 if (get_nodes_block(succ) == bl)
141 else /* count Keep-alive as one */
144 n_cfg_outs += succ->out[0].pos;
150 /* Access predecessor n, ignore keep-alives. */
151 ir_node *get_Block_cfg_out(const ir_node *bl, int pos)
154 assert(bl && is_Block(bl));
156 assert(bl->out_valid);
157 #endif /* defined DEBUG_libfirm */
158 for (i = 1; i <= bl->out[0].pos; ++i) {
159 ir_node *succ = bl->out[i].use;
160 if (get_irn_mode(succ) == mode_X && !is_End(succ)) {
161 int n_outs = succ->out[0].pos;
163 return succ->out[pos + 1].use;
171 /* Access predecessor n, honor keep-alives. */
172 ir_node *get_Block_cfg_out_ka(const ir_node *bl, int pos)
175 assert(bl && is_Block(bl));
177 assert (bl->out_valid);
178 #endif /* defined DEBUG_libfirm */
179 for (i = 1; i <= bl->out[0].pos; ++i) {
180 ir_node *succ = bl->out[i].use;
181 if (get_irn_mode(succ) == mode_X) {
183 ir_node *end_bl = get_nodes_block(succ);
185 /* ignore End if we are in the Endblock */
189 /* handle keep-alive here: return the Endblock instead of the End node */
194 n_outs = succ->out[0].pos;
196 return succ->out[pos + 1].use;
205 static void irg_out_walk_2(ir_node *node, irg_walk_func *pre,
206 irg_walk_func *post, void *env)
212 assert(get_irn_visited(node) < get_irg_visited(current_ir_graph));
214 set_irn_visited(node, get_irg_visited(current_ir_graph));
216 if (pre) pre(node, env);
218 for (i = 0, n = get_irn_n_outs(node); i < n; ++i) {
219 succ = get_irn_out(node, i);
220 if (get_irn_visited(succ) < get_irg_visited(current_ir_graph))
221 irg_out_walk_2(succ, pre, post, env);
224 if (post) post(node, env);
227 void irg_out_walk(ir_node *node, irg_walk_func *pre, irg_walk_func *post,
231 if (get_irg_outs_state(current_ir_graph) != outs_none) {
232 inc_irg_visited (current_ir_graph);
233 irg_out_walk_2(node, pre, post, env);
237 static void irg_out_block_walk2(ir_node *bl, irg_walk_func *pre,
238 irg_walk_func *post, void *env)
242 if (!Block_block_visited(bl)) {
243 mark_Block_block_visited(bl);
248 for (i = 0, n = get_Block_n_cfg_outs(bl); i < n; ++i) {
249 /* find the corresponding predecessor block. */
250 ir_node *pred = get_Block_cfg_out(bl, i);
252 irg_out_block_walk2(pred, pre, post, env);
260 /* Walks only over Block nodes in the graph. Has it's own visited
261 flag, so that it can be interleaved with the other walker. */
262 void irg_out_block_walk(ir_node *node, irg_walk_func *pre, irg_walk_func *post,
266 assert(is_Block(node) || (get_irn_mode(node) == mode_X));
268 inc_irg_block_visited(current_ir_graph);
270 if (get_irn_mode(node) == mode_X) {
273 for (i = 0, n = get_irn_n_outs(node); i < n; ++i) {
274 ir_node *succ = get_irn_out(node, i);
275 irg_out_block_walk2(succ, pre, post, env);
279 irg_out_block_walk2(node, pre, post, env);
283 /*--------------------------------------------------------------------*/
284 /** Building and Removing the out datastructure **/
286 /** The outs of a graph are allocated in a single, large array. **/
287 /** This allows to allocate and deallocate the memory for the outs **/
288 /** on demand. The large array is separated into many small ones **/
289 /** for each node. Only a single field to reference the out array **/
290 /** is stored in each node and a field referencing the large out **/
291 /** array in irgraph. The 0 field of each out array contains the **/
292 /** size of this array. This saves memory in the irnodes themselves.**/
293 /** The construction does two passes over the graph. The first pass **/
294 /** counts the overall number of outs and the outs of each node. It **/
295 /** stores the outs of each node in the out reference of the node. **/
296 /** Then the large array is allocated. The second iteration chops **/
297 /** the large array into smaller parts, sets the out edges and **/
298 /** recounts the out edges. **/
299 /** Removes Tuple nodes! **/
300 /*--------------------------------------------------------------------*/
303 /** Returns the amount of out edges for not yet visited successors. */
304 static int _count_outs(ir_node *n)
306 int start, i, res, irn_arity;
309 n->out = INT_TO_PTR(1); /* Space for array size. */
311 start = is_Block(n) ? 0 : -1;
312 irn_arity = get_irn_arity(n);
313 res = irn_arity - start + 1; /* --1 or --0; 1 for array size. */
315 for (i = start; i < irn_arity; ++i) {
316 /* Optimize Tuples. They annoy if walking the cfg. */
317 ir_node *pred = get_irn_n(n, i);
318 ir_node *skipped_pred = skip_Tuple(pred);
320 if (skipped_pred != pred) {
321 set_irn_n(n, i, skipped_pred);
324 /* count Def-Use edges for predecessors */
325 if (!irn_visited(skipped_pred))
326 res += _count_outs(skipped_pred);
328 /*count my Def-Use edges */
329 skipped_pred->out = INT_TO_PTR(PTR_TO_INT(skipped_pred->out) + 1);
335 /** Returns the amount of out edges for not yet visited successors.
336 * This version handles some special nodes like irg_frame, irg_args etc.
338 static int count_outs(ir_graph *irg)
343 inc_irg_visited(irg);
344 res = _count_outs(get_irg_end(irg));
346 /* Now handle anchored nodes. We need the out count of those
347 even if they are not visible. */
348 for (i = anchor_last - 1; i >= 0; --i) {
349 n = get_irg_anchor(irg, i);
350 if (!irn_visited_else_mark(n)) {
351 n->out = INT_TO_PTR(1);
359 * Enter memory for the outs to a node.
361 * @param use current node
362 * @param free current free address in the chunk allocated for the outs
364 * @return The next free address
366 static ir_def_use_edge *_set_out_edges(ir_node *use, ir_def_use_edge *free)
368 int n_outs, start, i, irn_arity, pos;
370 mark_irn_visited(use);
372 /* Allocate my array */
373 n_outs = PTR_TO_INT(use->out);
377 #endif /* defined DEBUG_libfirm */
379 /* We count the successors again, the space will be sufficient.
380 We use this counter to remember the position for the next back
384 start = is_Block(use) ? 0 : -1;
385 irn_arity = get_irn_arity(use);
387 for (i = start; i < irn_arity; ++i) {
388 ir_node *def = get_irn_n(use, i);
391 if (!irn_visited(def))
392 free = _set_out_edges(def, free);
394 /* Remember this Def-Use edge */
395 pos = def->out[0].pos + 1;
396 def->out[pos].use = use;
397 def->out[pos].pos = i;
399 /* increase the number of Def-Use edges so far */
400 def->out[0].pos = pos;
406 * Enter memory for the outs to a node. Handles special nodes
408 * @param irg the graph
409 * @param free current free address in the chunk allocated for the outs
411 * @return The next free address
413 static ir_def_use_edge *set_out_edges(ir_graph *irg, ir_def_use_edge *free)
418 inc_irg_visited(irg);
419 free = _set_out_edges(get_irg_end(irg), free);
421 /* handle anchored nodes */
422 for (i = anchor_last - 1; i >= 0; --i) {
423 n = get_irg_anchor(irg, i);
424 if (!irn_visited_else_mark(n)) {
425 n_outs = PTR_TO_INT(n->out);
429 #endif /* defined DEBUG_libfirm */
437 /* compute the outs for a given graph */
438 void compute_irg_outs(ir_graph *irg)
440 ir_graph *rem = current_ir_graph;
442 ir_def_use_edge *end = NULL; /* Only for debugging */
444 current_ir_graph = irg;
446 /* Update graph state */
447 assert(get_irg_phase_state(current_ir_graph) != phase_building);
449 if (current_ir_graph->outs_state != outs_none)
450 free_irg_outs(current_ir_graph);
452 /* This first iteration counts the overall number of out edges and the
453 number of out edges for each node. */
454 n_out_edges = count_outs(irg);
456 /* allocate memory for all out edges. */
457 irg->outs = XMALLOCNZ(ir_def_use_edge, n_out_edges);
459 irg->n_outs = n_out_edges;
460 #endif /* defined DEBUG_libfirm */
462 /* The second iteration splits the irg->outs array into smaller arrays
463 for each node and writes the back edges into this array. */
464 end = set_out_edges(irg, irg->outs);
466 /* Check how much memory we have used */
467 assert (end == (irg->outs + n_out_edges));
469 current_ir_graph->outs_state = outs_consistent;
470 current_ir_graph = rem;
473 void assure_irg_outs(ir_graph *irg)
475 if (get_irg_outs_state(irg) != outs_consistent)
476 compute_irg_outs(irg);
479 void compute_irp_outs(void)
482 for (i = get_irp_n_irgs() -1; i >= 0; --i)
483 compute_irg_outs(get_irp_irg(i));
486 void free_irp_outs(void)
489 for (i = get_irp_n_irgs() -1; i >= 0; --i)
490 free_irg_outs(get_irp_irg(i));
494 /*------------------------------------------------------------*
495 * This computes the outedges for in interprocedural graph. *
496 * There is one quirk: *
497 * The number of the outedges for each node is saved in *
498 * the first member of the ir_node** array. Maybe we should *
499 * change this to make it more portable... *
500 *------------------------------------------------------------*/
503 #ifdef INTERPROCEDURAL_VIEW
505 * Inits the number of outedges for each node
508 static void init_count(ir_node * node, void *env)
511 node->out = (ir_node **) 1; /* 1 for the array size */
516 * Adjusts the out edge count for its predecessors
517 * and adds the current arity to the overall count,
518 * which is saved in "env"
520 static void node_arity_count(ir_node * node, void * env)
522 int *anz = (int *) env, arity, n_outs, i, start;
525 arity = get_irn_arity(node);
526 start = (is_Block(node)) ? 0 : -1;
528 n_outs = 1 + arity + (-start); // ((is_Block(node)) ? 0 : 1); // Why + 1??
531 for (i = start; i < arity; i++) {
532 succ = get_irn_n(node, i);
533 succ->out = (ir_node **)INT_TO_PTR(PTR_TO_INT(succ->out) + 1);
538 * Inits all nodes for setting the outedges
539 * Returns the overall count of edges
541 int count_ip_outs(void)
545 cg_walk(init_count, node_arity_count, &res);
550 static int dummy_count = 0, global_count; /* Only for debugging */
553 * For each node: Sets the pointer to array
554 * in which the outedges are written later.
555 * The current array start is transported in env
557 static void set_array_pointer(ir_node *node, void *env)
560 ir_node ***free = (ir_node ***) env;
562 /* Allocate my array */
563 n_outs = PTR_TO_INT(node->out); /* We wrote the count here in count_ip_outs */
564 dummy_count += n_outs;
565 assert(dummy_count <= global_count && "More outedges than initially counted!");
567 *free = &((*free)[n_outs]);
568 /* We count the successors again, the space will be sufficient.
569 We use this counter to remember the position for the next back
571 node -> out[0] = (ir_node *) 0;
576 * Adds an outedge from the predecessor to the
579 static void set_out_pointer(ir_node * node, void *env)
581 int i, arity = get_irn_arity(node);
583 int start = (!is_Block(node)) ? -1 : 0;
586 for (i = start; i < arity; ++i) {
587 succ = get_irn_n(node, i);
588 succ->out[get_irn_n_outs(succ)+1] = node;
589 succ->out[0] = INT_TO_PTR(get_irn_n_outs(succ) + 1);
595 * Sets the outedges for all nodes.
597 void set_ip_outs(void)
599 ir_node **outedge_array = get_irp_ip_outedges();
600 cg_walk(set_array_pointer, set_out_pointer, (void *) &outedge_array);
606 * Counts the outedges, allocates memory to save the
607 * outedges and fills this outedge array in interprocedural
610 void compute_ip_outs(void)
615 assert(get_irp_ip_view_state() == ip_view_valid &&
616 "Cannot construct outs for invalid ip view.");
618 if (irp->outs_state != outs_none) {
622 global_count = n_out_edges = count_ip_outs();
623 out_edges = XMALLOCNZ(ir_node*, n_out_edges);
624 set_irp_ip_outedges(out_edges);
628 void free_ip_outs(void)
630 ir_node **out_edges = get_irp_ip_outedges();
631 if (out_edges != NULL) {
633 set_irp_ip_outedges(NULL);
635 irp->outs_state = outs_none;
640 void free_irg_outs(ir_graph *irg)
642 /* current_ir_graph->outs_state = outs_none; */
643 irg->outs_state = outs_none;
647 memset(irg->outs, 0, irg->n_outs);
648 #endif /* defined DEBUG_libfirm */
653 #endif /* defined DEBUG_libfirm */
657 /* when debugging, *always* reset all nodes' outs! irg->outs might
658 have been lying to us */
659 irg_walk_graph (irg, reset_outs, NULL, NULL);
660 #endif /* defined DEBUG_libfirm */