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 unsigned get_irn_n_outs(const ir_node *node)
43 return node->o.out->n_edges;
46 ir_node *get_irn_out(const ir_node *def, unsigned pos)
48 assert(pos < get_irn_n_outs(def));
49 return def->o.out->edges[pos].use;
52 ir_node *get_irn_out_ex(const ir_node *def, unsigned pos, int *in_pos)
54 assert(pos < get_irn_n_outs(def));
55 *in_pos = def->o.out->edges[pos].pos;
56 return def->o.out->edges[pos].use;
59 void set_irn_out(ir_node *def, unsigned pos, ir_node *use, int in_pos)
62 assert(pos < get_irn_n_outs(def));
63 def->o.out->edges[pos].use = use;
64 def->o.out->edges[pos].pos = in_pos;
67 unsigned get_Block_n_cfg_outs(const ir_node *bl)
70 unsigned n_cfg_outs = 0;
71 for (unsigned i = 0; i < get_irn_n_outs(bl); ++i) {
72 const ir_node *succ = get_irn_out(bl, i);
73 if (get_irn_mode(succ) != mode_X)
75 if (is_End(succ) || is_Bad(succ))
77 n_cfg_outs += get_irn_n_outs(succ);
82 unsigned get_Block_n_cfg_outs_ka(const ir_node *bl)
85 unsigned n_cfg_outs = 0;
86 for (unsigned i = 0; i < get_irn_n_outs(bl); ++i) {
87 const ir_node *succ = get_irn_out(bl, i);
88 if (get_irn_mode(succ) != mode_X)
93 ir_node *end_bl = get_nodes_block(succ);
99 n_cfg_outs += get_irn_n_outs(succ);
104 ir_node *get_Block_cfg_out(const ir_node *bl, unsigned pos)
106 assert(is_Block(bl));
107 for (unsigned i = 0; i < get_irn_n_outs(bl); ++i) {
108 const ir_node *succ = get_irn_out(bl, i);
109 if (get_irn_mode(succ) != mode_X)
111 if (is_End(succ) || is_Bad(succ))
114 unsigned n_outs = get_irn_n_outs(succ);
116 return get_irn_out(succ, pos);
123 ir_node *get_Block_cfg_out_ka(const ir_node *bl, unsigned pos)
125 assert(is_Block(bl));
126 for (unsigned i = 0; i < get_irn_n_outs(bl); ++i) {
127 const ir_node *succ = get_irn_out(bl, i);
128 if (get_irn_mode(succ) != mode_X)
134 ir_node *end_bl = get_nodes_block(succ);
136 /* ignore End if we are in the Endblock */
140 /* handle keep-alive here: return the Endblock instead of the End node */
147 unsigned n_outs = get_irn_n_outs(succ);
149 return get_irn_out(succ, pos);
156 static void irg_out_walk_2(ir_node *node, irg_walk_func *pre,
157 irg_walk_func *post, void *env)
159 assert(get_irn_visited(node) < get_irg_visited(current_ir_graph));
161 set_irn_visited(node, get_irg_visited(current_ir_graph));
163 if (pre) pre(node, env);
165 int n = get_irn_n_outs(node);
166 for (int i = 0; i < n; ++i) {
167 ir_node *succ = get_irn_out(node, i);
168 if (get_irn_visited(succ) < get_irg_visited(current_ir_graph))
169 irg_out_walk_2(succ, pre, post, env);
172 if (post) post(node, env);
175 void irg_out_walk(ir_node *node, irg_walk_func *pre, irg_walk_func *post,
179 ir_graph *irg = get_irn_irg(node);
180 if (irg_has_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUTS)) {
181 inc_irg_visited(irg);
182 irg_out_walk_2(node, pre, post, env);
186 static void irg_out_block_walk2(ir_node *bl, irg_walk_func *pre,
187 irg_walk_func *post, void *env)
189 if (Block_block_visited(bl))
192 mark_Block_block_visited(bl);
197 int n = get_Block_n_cfg_outs(bl);
198 for (int i = 0; i < n; ++i) {
199 /* find the corresponding predecessor block. */
200 ir_node *pred = get_Block_cfg_out(bl, i);
202 irg_out_block_walk2(pred, pre, post, env);
209 void irg_out_block_walk(ir_node *node, irg_walk_func *pre, irg_walk_func *post,
212 ir_graph *irg = get_irn_irg(node);
213 assert(is_Block(node) || (get_irn_mode(node) == mode_X));
215 ir_graph *rem = current_ir_graph;
216 current_ir_graph = irg;
218 inc_irg_block_visited(irg);
220 if (get_irn_mode(node) == mode_X) {
221 int n = get_irn_n_outs(node);
222 for (int i = 0; i < n; ++i) {
223 ir_node *succ = get_irn_out(node, i);
224 irg_out_block_walk2(succ, pre, post, env);
227 irg_out_block_walk2(node, pre, post, env);
230 current_ir_graph = rem;
233 /*--------------------------------------------------------------------*/
234 /** Building and Removing the out datastructure **/
236 /** The outs of a graph are allocated in a single, large array. **/
237 /** This allows to allocate and deallocate the memory for the outs **/
238 /** on demand. The large array is separated into many small ones **/
239 /** for each node. Only a single field to reference the out array **/
240 /** is stored in each node and a field referencing the large out **/
241 /** array in irgraph. The 0 field of each out array contains the **/
242 /** size of this array. This saves memory in the irnodes themselves.**/
243 /** The construction does two passes over the graph. The first pass **/
244 /** counts the overall number of outs and the outs of each node. It **/
245 /** stores the outs of each node in the out reference of the node. **/
246 /** Then the large array is allocated. The second iteration chops **/
247 /** the large array into smaller parts, sets the out edges and **/
248 /** recounts the out edges. **/
249 /** Removes Tuple nodes! **/
250 /*--------------------------------------------------------------------*/
253 /** Returns the amount of out edges for not yet visited successors. */
254 static void count_outs_node(ir_node *n)
256 if (irn_visited_else_mark(n))
259 /* initialize our counter */
262 int start = is_Block(n) ? 0 : -1;
263 int irn_arity = get_irn_arity(n);
264 for (int i = start; i < irn_arity; ++i) {
265 ir_node *def = get_irn_n(n, i);
266 /* optimize Tuples */
267 ir_node *skipped = skip_Tuple(def);
269 set_irn_n(n, i, skipped);
271 count_outs_node(skipped);
277 /** Returns the amount of out edges for not yet visited successors.
278 * This version handles some special nodes like irg_frame, irg_args etc. */
279 static void count_outs(ir_graph *irg)
281 inc_irg_visited(irg);
282 count_outs_node(get_irg_end(irg));
283 for (int i = anchor_first; i <= anchor_last; ++i) {
284 ir_node *n = get_irg_anchor(irg, i);
285 if (irn_visited_else_mark(n))
291 static void set_out_edges_node(ir_node *node, struct obstack *obst)
293 if (irn_visited_else_mark(node))
296 /* Allocate my array */
297 unsigned n_outs = node->o.n_outs;
298 node->o.out = OALLOCF(obst, ir_def_use_edges, edges, n_outs);
299 node->o.out->n_edges = 0;
301 /* add def->use edges from my predecessors to me */
302 int start = is_Block(node) ? 0 : -1;
303 int irn_arity = get_irn_arity(node);
304 for (int i = start; i < irn_arity; ++i) {
305 ir_node *def = get_irn_n(node, i);
307 /* recurse, ensures that out array of pred is already allocated */
308 set_out_edges_node(def, obst);
310 /* Remember this Def-Use edge */
311 unsigned pos = def->o.out->n_edges++;
312 def->o.out->edges[pos].use = node;
313 def->o.out->edges[pos].pos = i;
317 static void set_out_edges(ir_graph *irg)
319 struct obstack *obst = &irg->out_obst;
322 irg->out_obst_allocated = true;
324 inc_irg_visited(irg);
325 set_out_edges_node(get_irg_end(irg), obst);
326 for (int i = anchor_first; i <= anchor_last; ++i) {
327 ir_node *n = get_irg_anchor(irg, i);
328 if (irn_visited_else_mark(n))
330 n->o.out = OALLOCF(obst, ir_def_use_edges, edges, 0);
331 n->o.out->n_edges = 0;
335 void compute_irg_outs(ir_graph *irg)
339 /* This first iteration counts the overall number of out edges and the
340 number of out edges for each node. */
343 /* The second iteration splits the irg->outs array into smaller arrays
344 for each node and writes the back edges into this array. */
347 add_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUTS
348 | IR_GRAPH_PROPERTY_NO_TUPLES);
351 void assure_irg_outs(ir_graph *irg)
353 if (!irg_has_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUTS))
354 compute_irg_outs(irg);
358 /** Clear the outs of a node */
359 static void reset_outs(ir_node *node, void *unused)
366 void free_irg_outs(ir_graph *irg)
368 if (irg->out_obst_allocated) {
369 obstack_free(&irg->out_obst, NULL);
370 irg->out_obst_allocated = false;
374 /* when debugging, *always* reset all nodes' outs! irg->outs might
375 have been lying to us */
376 irg_walk_graph (irg, reset_outs, NULL, NULL);