3 * File name: ir/ana/irouts.c
4 * Purpose: Compute and access out edges.
5 * Author: Goetz Lindenmaier
9 * Copyright: (c) 2002-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
14 * @file irouts.c Compute out edges for ir nodes (also called def-use edges).
16 * Copyright (C) 2002 by Universitaet Karlsruhe
17 * All rights reserved.
19 * Authors: Goetz Lindenmaier
33 #include "irgraph_t.h"
38 /* Note: ir_node.out_valid and ir_graph.n_outs are only present when DEBUG_libfirm is defined */
39 /* Accesses to out_valid and n_outs are fenced out to avoid breakage
40 when compiling with neither DEBUG_libfirm or NDEBUG defined */
41 #endif /* defined DEBUG_libfirm */
43 /*--------------------------------------------------------------------*/
44 /** Accessing the out datastructures **/
45 /*--------------------------------------------------------------------*/
47 /** Clear the outs of a node */
48 static void reset_outs (ir_node *node, void *unused)
53 #endif /* defined DEBUG_libfirm */
56 /* returns the number of successors of the node: */
57 int get_irn_n_outs (ir_node *node) {
58 assert(node && node->kind == k_ir_node);
60 /* assert (node->out_valid); */
61 #endif /* defined DEBUG_libfirm */
62 return (int)(node->out[0]);
65 /* Access successor n */
66 ir_node *get_irn_out (ir_node *node, int pos) {
67 assert(pos >= 0 && pos < get_irn_n_outs(node));
69 /* assert (node->out_valid); */
70 #endif /* defined DEBUG_libfirm */
71 return node->out[pos+1];
74 void set_irn_out (ir_node *node, int pos, ir_node *out) {
76 assert(pos >= 0 && pos < get_irn_n_outs(node));
78 node->out_valid = 1; /* assume that this function is used correctly */
79 #endif /* defined DEBUG_libfirm */
80 node->out[pos+1] = out;
84 int get_Block_n_cfg_outs (ir_node *bl) {
85 int i, n_cfg_outs = 0;
86 assert(bl && (get_irn_op(bl) == op_Block));
88 assert (bl->out_valid);
89 #endif /* defined DEBUG_libfirm */
90 for (i = 0; i < (int)bl->out[0]; i++)
91 if ((get_irn_mode(bl->out[i+1]) == mode_X) &&
92 (get_irn_op(bl->out[i+1]) != op_End))
98 ir_node *get_Block_cfg_out (ir_node *bl, int pos) {
100 assert(bl && (get_irn_op(bl) == op_Block));
102 assert (bl->out_valid);
103 #endif /* defined DEBUG_libfirm */
104 for (i = 0; i < (int)bl->out[0]; i++)
105 if ((get_irn_mode(bl->out[i+1]) == mode_X) &&
106 (get_irn_op(bl->out[i+1]) != op_End)) {
107 if (out_pos == pos) {
108 ir_node *cfop = bl->out[i+1];
109 return cfop->out[0+1];
117 static void irg_out_walk_2(ir_node *node, irg_walk_func *pre,
118 irg_walk_func *post, void *env) {
123 assert(get_irn_visited(node) < get_irg_visited(current_ir_graph));
125 set_irn_visited(node, get_irg_visited(current_ir_graph));
127 if (pre) pre(node, env);
129 for (i = 0; i < get_irn_n_outs(node); i++) {
130 succ = get_irn_out(node, i);
131 if (get_irn_visited(succ) < get_irg_visited(current_ir_graph))
132 irg_out_walk_2(succ, pre, post, env);
135 if (post) post(node, env);
140 void irg_out_walk(ir_node *node,
141 irg_walk_func *pre, irg_walk_func *post,
144 if (get_irg_outs_state(current_ir_graph) != outs_none) {
145 inc_irg_visited (current_ir_graph);
146 irg_out_walk_2(node, pre, post, env);
151 static void irg_out_block_walk2(ir_node *bl,
152 irg_walk_func *pre, irg_walk_func *post,
156 if(get_Block_block_visited(bl) < get_irg_block_visited(current_ir_graph)) {
157 set_Block_block_visited(bl, get_irg_block_visited(current_ir_graph));
162 for(i = 0; i < get_Block_n_cfg_outs(bl); i++) {
163 /* find the corresponding predecessor block. */
164 ir_node *pred = get_Block_cfg_out(bl, i);
166 irg_out_block_walk2(pred, pre, post, env);
175 /* Walks only over Block nodes in the graph. Has it's own visited
176 flag, so that it can be interleaved with the other walker. */
177 void irg_out_block_walk(ir_node *node,
178 irg_walk_func *pre, irg_walk_func *post,
181 assert((get_irn_op(node) == op_Block) || (get_irn_mode(node) == mode_X));
183 inc_irg_block_visited(current_ir_graph);
185 if (get_irn_mode(node) == mode_X) node = node->out[1];
187 irg_out_block_walk2(node, pre, post, env);
193 /*--------------------------------------------------------------------*/
194 /** Building and Removing the out datasturcture **/
196 /** The outs of a graph are allocated in a single, large array. **/
197 /** This allows to allocate and deallocate the memory for the outs **/
198 /** on demand. The large array is separated into many small ones **/
199 /** for each node. Only a single field to reference the out array **/
200 /** is stored in each node and a field referencing the large out **/
201 /** array in irgraph. The 0 field of each out array contains the **/
202 /** size of this array. This saves memory in the irnodes themselves.**/
203 /** The construction does two passes over the graph. The first pass **/
204 /** counts the overall number of outs and the outs of each node. It **/
205 /** stores the outs of each node in the out reference of the node. **/
206 /** Then the large array is allocated. The second iteration chops **/
207 /** the large array into smaller parts, sets the out edges and **/
208 /** recounts the out edges. **/
209 /** Removes Tuple nodes! **/
210 /*--------------------------------------------------------------------*/
213 /** Returns the amount of out edges for not yet visited successors. */
214 static int _count_outs(ir_node *n) {
215 int start, i, res, irn_arity;
217 set_irn_visited(n, get_irg_visited(current_ir_graph));
218 n->out = (ir_node **) 1; /* Space for array size. */
220 start = is_Block(n) ? 0 : -1;
221 irn_arity = get_irn_arity(n);
222 res = irn_arity - start + 1; /* --1 or --0; 1 for array size. */
224 for (i = start; i < irn_arity; i++) {
225 /* Optimize Tuples. They annoy if walking the cfg. */
226 ir_node *succ = skip_Tuple(get_irn_n(n, i));
227 set_irn_n(n, i, succ);
229 /* count outs for successors */
230 if (get_irn_visited(succ) < get_irg_visited(current_ir_graph)) {
231 res += _count_outs(succ);
234 succ->out = (ir_node **)( (int)succ->out + 1);
240 /** Returns the amount of out edges for not yet visited successors.
241 * This version handles some special nodes like irg_frame etc.
243 static int count_outs(ir_graph *irg) {
247 inc_irg_visited(irg);
248 res = _count_outs(get_irg_end(irg));
250 /* now handle special nodes */
251 n = get_irg_frame(irg);
252 if (get_irn_visited(n) < get_irg_visited(current_ir_graph)) {
253 n->out = (ir_node **)1;
261 * Enter memory for the outs to a node.
263 * @param n current node
264 * @param free current free address in the chunk allocated for the outs
266 * @return The next free address
268 static ir_node **_set_out_edges(ir_node *n, ir_node **free) {
269 int n_outs, start, i, irn_arity;
272 set_irn_visited(n, get_irg_visited(current_ir_graph));
274 /* Allocate my array */
275 n_outs = (int) n->out;
279 #endif /* defined DEBUG_libfirm */
281 /* We count the successors again, the space will be sufficient.
282 We use this counter to remember the position for the next back
284 n->out[0] = (ir_node *)0;
286 start = is_Block(n) ? 0 : -1;
287 irn_arity = get_irn_arity(n);
289 for (i = start; i < irn_arity; i++) {
290 succ = get_irn_n(n, i);
292 if (get_irn_visited(succ) < get_irg_visited(current_ir_graph))
293 free = _set_out_edges(succ, free);
294 /* Remember our back edge */
295 succ->out[get_irn_n_outs(succ)+1] = n;
296 succ->out[0] = (ir_node *) (get_irn_n_outs(succ) + 1);
302 * Enter memory for the outs to a node. Handles special nodes
304 * @param irg the graph
305 * @param free current free address in the chunk allocated for the outs
307 * @return The next free address
309 static ir_node **set_out_edges(ir_graph *irg, ir_node **free) {
313 inc_irg_visited(irg);
314 free = _set_out_edges(get_irg_end(irg), free);
316 n = get_irg_frame(irg);
317 if (get_irn_visited(n) < get_irg_visited(current_ir_graph)) {
318 n_outs = (int)n->out;
322 #endif /* defined DEBUG_libfirm */
330 /* We want that the out of ProjX from Start contains the next block at
331 position 1, the Start block at position 2. This is necessary for
332 the out block walker. */
333 static INLINE void fix_start_proj(ir_graph *irg) {
334 ir_node *proj = NULL;
335 ir_node *startbl = get_irg_start_block(irg);
338 if (get_Block_n_cfg_outs(startbl)) {
339 for (i = 0; i < get_irn_n_outs(startbl); i++)
340 if (get_irn_mode(get_irn_out(startbl, i)) == mode_X) {
341 proj = get_irn_out(startbl, i);
345 if (get_irn_out(proj, 0) == startbl) {
346 assert(get_irn_n_outs(proj) == 2);
347 set_irn_out(proj, 0, get_irn_out(proj, 1));
348 set_irn_out(proj, 1, startbl);
353 /* compute the outs for a given graph */
354 void compute_outs(ir_graph *irg) {
355 ir_graph *rem = current_ir_graph;
357 ir_node **end = NULL; /* Only for debugging */
359 current_ir_graph = irg;
361 /* Update graph state */
362 assert(get_irg_phase_state(current_ir_graph) != phase_building);
364 if (current_ir_graph->outs_state != outs_none)
365 free_outs(current_ir_graph);
366 current_ir_graph->outs_state = outs_consistent;
368 /* This first iteration counts the overall number of out edges and the
369 number of out edges for each node. */
370 n_out_edges = count_outs(irg);
372 /* allocate memory for all out edges. */
373 irg->outs = xcalloc(n_out_edges, sizeof(irg->outs[0]));
375 irg->n_outs = n_out_edges;
376 #endif /* defined DEBUG_libfirm */
378 /* The second iteration splits the irg->outs array into smaller arrays
379 for each node and writes the back edges into this array. */
380 end = set_out_edges(irg, irg->outs);
382 /* Check how much memory we have used */
383 assert (end == (irg->outs + n_out_edges));
385 /* We want that the out of ProjX from Start contains the next block at
386 position 1, the Start block at position 2. This is necessary for
387 the out block walker. */
390 current_ir_graph = rem;
396 /*------------------------------------------------------------*
397 * This computes the outedges for in interprocedural graph. *
398 * There is one quirk: *
399 * The number of the outedges for each node is saved in *
400 * the first member of the ir_node** array. Maybe we should *
401 * change this to make it more portable... *
402 *------------------------------------------------------------*/
406 * Inits the number of outedges for each node
409 static void init_count(ir_node * node, void *env) {
410 node->out = (ir_node **) 1; /* 1 for the array size */
415 * Adjusts the out edge count for its predecessors
416 * and adds the current arity to the overall count,
417 * which is saved in "env"
419 static void node_arity_count(ir_node * node, void * env)
421 int *anz = (int *) env, arity, n_outs, i, start;
424 arity = get_irn_arity(node);
425 start = (is_Block(node)) ? 0 : -1;
427 n_outs = 1 + arity + (-start); // ((is_Block(node)) ? 0 : 1); // Why + 1??
430 for(i = start; i < arity; i++) {
431 succ = get_irn_n(node, i);
432 succ->out = (ir_node **)((int)succ->out + 1);
438 * Inits all nodes for setting the outedges
439 * Returns the overall count of edges
441 int count_ip_outs(void) {
445 cg_walk(init_count, node_arity_count, &res);
450 static int dummy_count = 0, global_count; /* Only for debugging */
453 * For each node: Sets the pointer to array
454 * in which the outedges are written later.
455 * The current array start is transported in env
457 static void set_array_pointer(ir_node *node, void *env) {
460 ir_node ***free = (ir_node ***) env;
462 /* Allocate my array */
463 n_outs = (int) node -> out; /* We wrote the count here in count_ip_outs */
464 dummy_count += n_outs;
465 assert(dummy_count <= global_count && "More outedges than initially counted!");
467 *free = &((*free)[n_outs]);
468 /* We count the successors again, the space will be sufficient.
469 We use this counter to remember the position for the next back
471 node -> out[0] = (ir_node *) 0;
476 * Adds an outedge from the predecessor to the
479 static void set_out_pointer(ir_node * node, void * env) {
480 int i, arity = get_irn_arity(node);
482 int start = (!is_Block(node)) ? -1 : 0;
484 for(i = start; i < arity; i++) {
485 succ = get_irn_n(node, i);
486 succ->out[get_irn_n_outs(succ)+1] = node;
487 succ->out[0] = (ir_node *) (get_irn_n_outs(succ) + 1);
493 * Sets the outedges for all nodes.
495 void set_ip_outs(void)
497 ir_node **outedge_array = get_irp_ip_outedges();
498 cg_walk(set_array_pointer, set_out_pointer, (void *) &outedge_array);
504 * Counts the outedges, allocates memory to save the
505 * outedges and fills this outedge array in interprocedural
508 void compute_ip_outs(void) {
513 assert(get_irp_ip_view_state() == ip_view_valid &&
514 "Cannot construct outs for invalid ip view.");
516 if (irp->outs_state != outs_none) {
520 global_count = n_out_edges = count_ip_outs();
521 out_edges = xcalloc(n_out_edges, sizeof(out_edges[0]));
522 set_irp_ip_outedges(out_edges);
526 void free_ip_outs(void)
528 ir_node **out_edges = get_irp_ip_outedges();
529 if (out_edges != NULL) {
531 set_irp_ip_outedges(NULL);
533 irp->outs_state = outs_none;
537 void free_outs(ir_graph *irg) {
539 /* current_ir_graph->outs_state = outs_none; */
540 irg->outs_state = outs_none;
544 memset(irg->outs, 0, irg->n_outs);
545 #endif /* defined DEBUG_libfirm */
550 #endif /* defined DEBUG_libfirm */
554 /* when debugging, *always* reset all nodes' outs! irg->outs might
555 have been lying to us */
556 irg_walk_graph (irg, reset_outs, NULL, NULL);
557 #endif /* defined DEBUG_libfirm */