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"
41 /*--------------------------------------------------------------------*/
42 /** Accessing the out datastructures **/
43 /*--------------------------------------------------------------------*/
46 /** Clear the outs of a node */
47 static void reset_outs(ir_node *node, void *unused)
55 int get_irn_outs_computed(const ir_node *node)
57 return node->out != NULL;
60 int get_irn_n_outs(const ir_node *node)
62 assert(node && node->kind == k_ir_node);
64 assert(node->out_valid);
65 #endif /* defined DEBUG_libfirm */
66 /* we misuse the first for the size info of the out array */
67 return node->out[0].pos;
70 ir_node *get_irn_out(const ir_node *def, int pos)
72 assert(pos >= 0 && pos < get_irn_n_outs(def));
74 assert(def->out_valid);
75 #endif /* defined DEBUG_libfirm */
76 return def->out[pos+1].use;
79 ir_node *get_irn_out_ex(const ir_node *def, int pos, int *in_pos)
81 assert(pos >= 0 && pos < get_irn_n_outs(def));
83 assert(def->out_valid);
84 #endif /* defined DEBUG_libfirm */
85 *in_pos = def->out[pos+1].pos;
86 return def->out[pos+1].use;
89 void set_irn_out(ir_node *def, int pos, ir_node *use, int in_pos)
92 assert(pos >= 0 && pos < get_irn_n_outs(def));
94 assert(def->out_valid);
95 #endif /* defined DEBUG_libfirm */
96 def->out[pos+1].use = use;
97 def->out[pos+1].pos = in_pos;
100 int get_Block_n_cfg_outs(const ir_node *bl)
102 int i, n_cfg_outs = 0;
103 assert(bl && is_Block(bl));
105 assert(bl->out_valid);
106 #endif /* defined DEBUG_libfirm */
107 for (i = 1; i <= bl->out[0].pos; ++i) {
108 ir_node *succ = bl->out[i].use;
109 if (get_irn_mode(succ) == mode_X && !is_End(succ) && !is_Bad(succ))
110 n_cfg_outs += succ->out[0].pos;
115 int get_Block_n_cfg_outs_ka(const ir_node *bl)
117 int i, n_cfg_outs = 0;
118 assert(bl && is_Block(bl));
120 assert(bl->out_valid);
121 #endif /* defined DEBUG_libfirm */
122 for (i = 1; i <= bl->out[0].pos; ++i) {
123 ir_node *succ = bl->out[i].use;
124 if (get_irn_mode(succ) == mode_X) {
128 /* ignore End if we are in the Endblock */
129 if (get_nodes_block(succ) == bl)
131 else /* count Keep-alive as one */
134 n_cfg_outs += succ->out[0].pos;
140 ir_node *get_Block_cfg_out(const ir_node *bl, int pos)
143 assert(bl && is_Block(bl));
145 assert(bl->out_valid);
146 #endif /* defined DEBUG_libfirm */
147 for (i = 1; i <= bl->out[0].pos; ++i) {
148 ir_node *succ = bl->out[i].use;
149 if (get_irn_mode(succ) == mode_X && !is_End(succ) && !is_Bad(succ)) {
150 int n_outs = succ->out[0].pos;
152 return succ->out[pos + 1].use;
160 ir_node *get_Block_cfg_out_ka(const ir_node *bl, int pos)
163 assert(bl && is_Block(bl));
165 assert (bl->out_valid);
166 #endif /* defined DEBUG_libfirm */
167 for (i = 1; i <= bl->out[0].pos; ++i) {
168 ir_node *succ = bl->out[i].use;
169 if (get_irn_mode(succ) == mode_X) {
173 ir_node *end_bl = get_nodes_block(succ);
175 /* ignore End if we are in the Endblock */
179 /* handle keep-alive here: return the Endblock instead of the End node */
184 n_outs = succ->out[0].pos;
186 return succ->out[pos + 1].use;
195 static void irg_out_walk_2(ir_node *node, irg_walk_func *pre,
196 irg_walk_func *post, void *env)
202 assert(get_irn_visited(node) < get_irg_visited(current_ir_graph));
204 set_irn_visited(node, get_irg_visited(current_ir_graph));
206 if (pre) pre(node, env);
208 for (i = 0, n = get_irn_n_outs(node); i < n; ++i) {
209 succ = get_irn_out(node, i);
210 if (get_irn_visited(succ) < get_irg_visited(current_ir_graph))
211 irg_out_walk_2(succ, pre, post, env);
214 if (post) post(node, env);
217 void irg_out_walk(ir_node *node, irg_walk_func *pre, irg_walk_func *post,
221 ir_graph *irg = get_irn_irg(node);
222 if (is_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_OUTS)) {
223 inc_irg_visited (irg);
224 irg_out_walk_2(node, pre, post, env);
228 static void irg_out_block_walk2(ir_node *bl, irg_walk_func *pre,
229 irg_walk_func *post, void *env)
233 if (!Block_block_visited(bl)) {
234 mark_Block_block_visited(bl);
239 for (i = 0, n = get_Block_n_cfg_outs(bl); i < n; ++i) {
240 /* find the corresponding predecessor block. */
241 ir_node *pred = get_Block_cfg_out(bl, i);
243 irg_out_block_walk2(pred, pre, post, env);
251 void irg_out_block_walk(ir_node *node, irg_walk_func *pre, irg_walk_func *post,
255 assert(is_Block(node) || (get_irn_mode(node) == mode_X));
257 inc_irg_block_visited(current_ir_graph);
259 if (get_irn_mode(node) == mode_X) {
262 for (i = 0, n = get_irn_n_outs(node); i < n; ++i) {
263 ir_node *succ = get_irn_out(node, i);
264 irg_out_block_walk2(succ, pre, post, env);
268 irg_out_block_walk2(node, pre, post, env);
272 /*--------------------------------------------------------------------*/
273 /** Building and Removing the out datastructure **/
275 /** The outs of a graph are allocated in a single, large array. **/
276 /** This allows to allocate and deallocate the memory for the outs **/
277 /** on demand. The large array is separated into many small ones **/
278 /** for each node. Only a single field to reference the out array **/
279 /** is stored in each node and a field referencing the large out **/
280 /** array in irgraph. The 0 field of each out array contains the **/
281 /** size of this array. This saves memory in the irnodes themselves.**/
282 /** The construction does two passes over the graph. The first pass **/
283 /** counts the overall number of outs and the outs of each node. It **/
284 /** stores the outs of each node in the out reference of the node. **/
285 /** Then the large array is allocated. The second iteration chops **/
286 /** the large array into smaller parts, sets the out edges and **/
287 /** recounts the out edges. **/
288 /** Removes Tuple nodes! **/
289 /*--------------------------------------------------------------------*/
292 /** Returns the amount of out edges for not yet visited successors. */
293 static int _count_outs(ir_node *n)
295 int start, i, res, irn_arity;
298 n->out = (ir_def_use_edge*) INT_TO_PTR(1); /* Space for array size. */
300 start = is_Block(n) ? 0 : -1;
301 irn_arity = get_irn_arity(n);
302 res = irn_arity - start + 1; /* --1 or --0; 1 for array size. */
304 for (i = start; i < irn_arity; ++i) {
305 /* Optimize Tuples. They annoy if walking the cfg. */
306 ir_node *pred = get_irn_n(n, i);
307 ir_node *skipped_pred = skip_Tuple(pred);
309 if (skipped_pred != pred) {
310 set_irn_n(n, i, skipped_pred);
313 /* count Def-Use edges for predecessors */
314 if (!irn_visited(skipped_pred))
315 res += _count_outs(skipped_pred);
317 /*count my Def-Use edges */
318 skipped_pred->out = (ir_def_use_edge*) INT_TO_PTR(PTR_TO_INT(skipped_pred->out) + 1);
324 /** Returns the amount of out edges for not yet visited successors.
325 * This version handles some special nodes like irg_frame, irg_args etc.
327 static int count_outs(ir_graph *irg)
332 inc_irg_visited(irg);
333 res = _count_outs(get_irg_end(irg));
335 /* Now handle anchored nodes. We need the out count of those
336 even if they are not visible. */
337 for (i = anchor_last; i >= anchor_first; --i) {
338 n = get_irg_anchor(irg, i);
339 if (!irn_visited_else_mark(n)) {
340 n->out = (ir_def_use_edge*) INT_TO_PTR(1);
348 * Enter memory for the outs to a node.
350 * @param use current node
351 * @param free current free address in the chunk allocated for the outs
353 * @return The next free address
355 static ir_def_use_edge *_set_out_edges(ir_node *use, ir_def_use_edge *free)
357 int start, i, irn_arity, pos;
360 mark_irn_visited(use);
362 /* Allocate my array */
363 n_outs = PTR_TO_INT(use->out);
367 #endif /* defined DEBUG_libfirm */
369 /* We count the successors again, the space will be sufficient.
370 We use this counter to remember the position for the next back
374 start = is_Block(use) ? 0 : -1;
375 irn_arity = get_irn_arity(use);
377 for (i = start; i < irn_arity; ++i) {
378 ir_node *def = get_irn_n(use, i);
381 if (!irn_visited(def))
382 free = _set_out_edges(def, free);
384 /* Remember this Def-Use edge */
385 pos = def->out[0].pos + 1;
386 def->out[pos].use = use;
387 def->out[pos].pos = i;
389 /* increase the number of Def-Use edges so far */
390 def->out[0].pos = pos;
396 * Enter memory for the outs to a node. Handles special nodes
398 * @param irg the graph
399 * @param free current free address in the chunk allocated for the outs
401 * @return The next free address
403 static ir_def_use_edge *set_out_edges(ir_graph *irg, ir_def_use_edge *free)
408 inc_irg_visited(irg);
409 free = _set_out_edges(get_irg_end(irg), free);
411 /* handle anchored nodes */
412 for (i = anchor_last; i >= anchor_first; --i) {
413 n = get_irg_anchor(irg, i);
414 if (!irn_visited_else_mark(n)) {
415 size_t n_outs = PTR_TO_INT(n->out);
419 #endif /* defined DEBUG_libfirm */
427 void compute_irg_outs(ir_graph *irg)
429 ir_graph *rem = current_ir_graph;
431 ir_def_use_edge *end = NULL; /* Only for debugging */
433 current_ir_graph = irg;
435 /* Update graph state */
436 assert(get_irg_phase_state(current_ir_graph) != phase_building);
438 free_irg_outs(current_ir_graph);
440 /* This first iteration counts the overall number of out edges and the
441 number of out edges for each node. */
442 n_out_edges = count_outs(irg);
444 /* allocate memory for all out edges. */
445 irg->outs = XMALLOCNZ(ir_def_use_edge, n_out_edges);
447 irg->n_outs = n_out_edges;
448 #endif /* defined DEBUG_libfirm */
450 /* The second iteration splits the irg->outs array into smaller arrays
451 for each node and writes the back edges into this array. */
452 end = set_out_edges(irg, irg->outs);
454 /* Check how much memory we have used */
455 assert (end == (irg->outs + n_out_edges));
457 set_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_OUTS);
458 current_ir_graph = rem;
461 void assure_irg_outs(ir_graph *irg)
463 if (! is_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_OUTS))
464 compute_irg_outs(irg);
467 void compute_irp_outs(void)
470 for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
471 compute_irg_outs(get_irp_irg(i));
474 void free_irp_outs(void)
477 for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
478 free_irg_outs(get_irp_irg(i));
481 void free_irg_outs(ir_graph *irg)
483 /* current_ir_graph->outs_state = outs_none; */
487 memset(irg->outs, 0, irg->n_outs);
488 #endif /* defined DEBUG_libfirm */
493 #endif /* defined DEBUG_libfirm */
497 /* when debugging, *always* reset all nodes' outs! irg->outs might
498 have been lying to us */
499 irg_walk_graph (irg, reset_outs, NULL, NULL);
500 #endif /* defined DEBUG_libfirm */