* @brief Functions for traversing ir graphs
* @author Boris Boesler, Goetz Lindenmaier, Michael Beck
* @version $Id$
- * @summary
+ * @brief
* traverse an ir graph
* - execute the pre function before recursion
* - execute the post function after recursion
*/
#include "config.h"
-#ifdef HAVE_STDLIB_H
-# include <stdlib.h>
-#endif
+#include <stdlib.h>
#include "irnode_t.h"
#include "irgraph_t.h"
#include "pset_new.h"
#include "array.h"
+#ifdef INTERPROCEDURAL_VIEW
/**
* Walk over an interprocedural graph (callgraph).
* Visits only graphs in irg_set.
*/
static void irg_walk_cg(ir_node * node, ir_visited_t visited,
pset_new_t *irg_set, irg_walk_func *pre,
- irg_walk_func *post, void * env) {
+ irg_walk_func *post, void * env)
+{
int i;
ir_graph * rem = current_ir_graph;
ir_node * pred;
current_ir_graph = rem;
}
-
/**
* Insert all ir_graphs in irg_set, that are (transitive) reachable.
*/
-static void collect_irgs(ir_node * node, pset_new_t *irg_set) {
+static void collect_irgs(ir_node * node, pset_new_t *irg_set)
+{
if (is_Call(node)) {
int i;
for (i = get_Call_n_callees(node) - 1; i >= 0; --i) {
}
}
}
+#endif
/**
* specialized version of irg_walk_2, called if only pre callback exists
*
* @return number of visited nodes
*/
-static unsigned
-irg_walk_2_pre(ir_node *node, irg_walk_func *pre, void * env) {
+static unsigned irg_walk_2_pre(ir_node *node, irg_walk_func *pre, void *env)
+{
int i;
unsigned cnt = 1;
- ir_graph *irg = current_ir_graph;
+ ir_graph *irg = get_irn_irg(node);
set_irn_visited(node, irg->visited);
*
* @return number of visited nodes
*/
-static unsigned
-irg_walk_2_post(ir_node *node, irg_walk_func *post, void * env) {
+static unsigned irg_walk_2_post(ir_node *node, irg_walk_func *post, void *env)
+{
int i;
unsigned cnt = 1;
- ir_graph *irg = current_ir_graph;
+ ir_graph *irg = get_irn_irg(node);
set_irn_visited(node, irg->visited);
*
* @return number of visited nodes
*/
-static unsigned
-irg_walk_2_both(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void * env) {
+static unsigned irg_walk_2_both(ir_node *node, irg_walk_func *pre,
+ irg_walk_func *post, void *env)
+{
int i;
unsigned cnt = 1;
- ir_graph *irg = current_ir_graph;
+ ir_graph *irg = get_irn_irg(node);
set_irn_visited(node, irg->visited);
*
* @return number of visited nodes
*/
-static unsigned
-irg_walk_2(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void * env)
+unsigned irg_walk_2(ir_node *node, irg_walk_func *pre, irg_walk_func *post,
+ void *env)
{
- if (node->visited < current_ir_graph->visited) {
- if (!post) return irg_walk_2_pre (node, pre, env);
- else if (!pre) return irg_walk_2_post(node, post, env);
- else return irg_walk_2_both(node, pre, post, env);
- }
- return 0;
+ if (irn_visited(node))
+ return 0;
+
+ if (!post) return irg_walk_2_pre (node, pre, env);
+ else if (!pre) return irg_walk_2_post(node, post, env);
+ else return irg_walk_2_both(node, pre, post, env);
}
/* a counter */
static unsigned nodes_touched = 0;
-/*
- * generic graph walker
- */
-void irg_walk(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void *env)
+void irg_walk_core(ir_node *node, irg_walk_func *pre, irg_walk_func *post,
+ void *env)
{
assert(is_ir_node(node));
pset_new_destroy(&irg_set);
} else {
#endif
- ir_reserve_resources(current_ir_graph, IR_RESOURCE_IRN_VISITED);
- inc_irg_visited(current_ir_graph);
nodes_touched = irg_walk_2(node, pre, post, env);
- ir_free_resources(current_ir_graph, IR_RESOURCE_IRN_VISITED);
#ifdef INTERPROCEDURAL_VIEW
}
#endif
}
+void irg_walk(ir_node *node, irg_walk_func *pre, irg_walk_func *post,
+ void *env)
+{
+ ir_reserve_resources(current_ir_graph, IR_RESOURCE_IRN_VISITED);
+ inc_irg_visited(current_ir_graph);
+ assert(current_ir_graph == get_irn_irg(node));
+ irg_walk_core(node, pre, post, env);
+ ir_free_resources(current_ir_graph, IR_RESOURCE_IRN_VISITED);
+}
+
/*
* walk over a graph
*/
-void irg_walk_graph(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env) {
+void irg_walk_graph(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env)
+{
ir_graph * rem = current_ir_graph;
hook_irg_walk(irg, (generic_func *)pre, (generic_func *)post);
/* Executes irg_walk(end, pre, post, env) for all irgraphs in irprog.
Sets current_ir_graph properly for each walk. Conserves current
current_ir_graph. */
-void all_irg_walk(irg_walk_func *pre, irg_walk_func *post, void *env) {
+void all_irg_walk(irg_walk_func *pre, irg_walk_func *post, void *env)
+{
int i, n;
ir_graph *irg;
*
* @return number of visited nodes
*/
-static unsigned
-irg_walk_in_or_dep_2_pre(ir_node *node, irg_walk_func *pre, void *env) {
+static unsigned irg_walk_in_or_dep_2_pre(ir_node *node, irg_walk_func *pre, void *env)
+{
int i;
unsigned cnt = 1;
- ir_graph *irg = current_ir_graph;
+ ir_graph *irg = get_irn_irg(node);
set_irn_visited(node, irg->visited);
*
* @return number of visited nodes
*/
-static unsigned
-irg_walk_in_or_dep_2_post(ir_node *node, irg_walk_func *post, void *env) {
+static unsigned irg_walk_in_or_dep_2_post(ir_node *node, irg_walk_func *post, void *env)
+{
int i;
unsigned cnt = 1;
- ir_graph *irg = current_ir_graph;
+ ir_graph *irg = get_irn_irg(node);
set_irn_visited(node, irg->visited);
*
* @return number of visited nodes
*/
-static unsigned
-irg_walk_in_or_dep_2_both(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void *env) {
+static unsigned irg_walk_in_or_dep_2_both(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void *env)
+{
int i;
unsigned cnt = 1;
- ir_graph *irg = current_ir_graph;
+ ir_graph *irg = get_irn_irg(node);
set_irn_visited(node, irg->visited);
*
* @return number of visited nodes
*/
-static unsigned
-irg_walk_in_or_dep_2(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void *env)
+static unsigned irg_walk_in_or_dep_2(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void *env)
{
- if (node->visited < current_ir_graph->visited) {
- if (! post) return irg_walk_in_or_dep_2_pre (node, pre, env);
- else if (! pre) return irg_walk_in_or_dep_2_post(node, post, env);
- else return irg_walk_in_or_dep_2_both(node, pre, post, env);
- }
- return 0;
+ if (irn_visited(node))
+ return 0;
+
+ if (! post) return irg_walk_in_or_dep_2_pre (node, pre, env);
+ else if (! pre) return irg_walk_in_or_dep_2_post(node, post, env);
+ else return irg_walk_in_or_dep_2_both(node, pre, post, env);
}
/*
/*
* Walk over a graph. Follow all edges (including dependencies)
*/
-void irg_walk_in_or_dep_graph(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env) {
+void irg_walk_in_or_dep_graph(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env)
+{
ir_graph * rem = current_ir_graph;
hook_irg_walk(irg, (generic_func *)pre, (generic_func *)post);
* Returns current_ir_graph and sets it to the irg of predecessor index
* of node n.
*/
-static INLINE ir_graph *
-switch_irg(ir_node *n, int index) {
+static inline ir_graph * switch_irg(ir_node *n, int index)
+{
ir_graph *old_current = current_ir_graph;
if (get_interprocedural_view()) {
return old_current;
}
-static void
-cg_walk_2(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void * env)
+#ifdef INTERPROCEDURAL_VIEW
+static void cg_walk_2(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void * env)
{
int i;
ir_graph *rem = NULL;
}
}
-#ifdef INTERPROCEDURAL_VIEW
/* Walks all irgs in interprocedural view. Visits each node only once. */
-void cg_walk(irg_walk_func *pre, irg_walk_func *post, void *env) {
+void cg_walk(irg_walk_func *pre, irg_walk_func *post, void *env)
+{
int i;
ir_graph *rem = current_ir_graph;
int rem_view = get_interprocedural_view();
/***************************************************************************/
/* Walks back from n until it finds a real cf op. */
-static ir_node *get_cf_op(ir_node *n) {
+static ir_node *get_cf_op(ir_node *n)
+{
while (!is_cfop(n) && !is_fragile_op(n) && !is_Bad(n)) {
n = skip_Id(n);
n = skip_Tuple(n);
return n;
}
-static void irg_block_walk_2(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void *env)
+static void irg_block_walk_2(ir_node *node, irg_walk_func *pre,
+ irg_walk_func *post, void *env)
{
int i;
- if (!Block_block_visited(node)) {
- mark_Block_block_visited(node);
+ if (Block_block_visited(node))
+ return;
+ mark_Block_block_visited(node);
- if(pre) pre(node, env);
+ if (pre)
+ pre(node, env);
- for(i = get_Block_n_cfgpreds(node) - 1; i >= 0; --i) {
- /* find the corresponding predecessor block. */
- ir_node *pred = get_cf_op(get_Block_cfgpred(node, i));
- pred = get_nodes_block(pred);
- if(get_irn_opcode(pred) == iro_Block) {
- /* recursion */
- irg_block_walk_2(pred, pre, post, env);
- }
- else {
- assert(get_irn_opcode(pred) == iro_Bad);
- }
+ for (i = get_Block_n_cfgpreds(node) - 1; i >= 0; --i) {
+ /* find the corresponding predecessor block. */
+ ir_node *pred = get_cf_op(get_Block_cfgpred(node, i));
+ pred = get_nodes_block(pred);
+ if (get_irn_opcode(pred) == iro_Block) {
+ /* recursion */
+ irg_block_walk_2(pred, pre, post, env);
+ } else {
+ assert(get_irn_opcode(pred) == iro_Bad);
}
-
- if(post) post(node, env);
}
+
+ if (post)
+ post(node, env);
}
flag, so that it can be interleaved with the other walker. */
void irg_block_walk(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void *env)
{
- ir_graph *irg = current_ir_graph;
+ ir_graph *irg = get_irn_irg(node);
ir_node *block, *pred;
int i;
assert(node);
assert(!get_interprocedural_view()); /* interprocedural_view not implemented, because it
- * interleaves with irg_walk */
+ * interleaves with irg_walk */
ir_reserve_resources(irg, IR_RESOURCE_BLOCK_VISITED);
inc_irg_block_visited(irg);
block = is_Block(node) ? node : get_nodes_block(node);
int arity = get_irn_arity(node);
for (i = 0; i < arity; i++) {
pred = get_irn_n(node, i);
- if (is_Block(pred))
- irg_block_walk_2(pred, pre, post, env);
- }
- /* Sometimes the blocks died, but are still reachable through Phis.
- * Make sure the algorithms that try to remove these reach them. */
- for (i = 0; i < arity; i++) {
- pred = get_irn_n(node, i);
- if (get_irn_op(pred) == op_Phi) {
- ir_node *block = get_nodes_block(pred);
-
- if (! is_Bad(block))
- irg_block_walk_2(block, pre, post, env);
+ if (!is_Block(pred)) {
+ pred = get_nodes_block(pred);
+ if (!is_Block(pred)) {
+ /* if rare cases a kept node might have a bad block input */
+ continue;
+ }
}
+ /* Sometimes the blocks died, but are still reachable through kept nodes.
+ * Make sure the algorithms that try to remove these reach them. */
+ irg_block_walk_2(pred, pre, post, env);
}
}
* walk over a graph block wise
*/
void irg_block_walk_graph(ir_graph *irg, irg_walk_func *pre,
- irg_walk_func *post, void *env) {
+ irg_walk_func *post, void *env)
+{
ir_graph * rem = current_ir_graph;
current_ir_graph = irg;
irg_block_walk(get_irg_end(irg), pre, post, env);
/*
* Additionally walk over all anchors. Do NOT increase the visit flag.
*/
-void irg_walk_anchors(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env) {
+void irg_walk_anchors(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env)
+{
ir_graph * rem = current_ir_graph;
current_ir_graph = irg;
static void walk_initializer(ir_initializer_t *initializer, walk_env *env)
{
- switch(initializer->kind) {
+ switch (initializer->kind) {
case IR_INITIALIZER_CONST:
irg_walk(initializer->consti.value, env->pre, env->post, env->env);
return;
case IR_INITIALIZER_COMPOUND: {
size_t i;
- for(i = 0; i < initializer->compound.n_initializers; ++i) {
+ for (i = 0; i < initializer->compound.n_initializers; ++i) {
ir_initializer_t *subinitializer
= initializer->compound.initializers[i];
walk_initializer(subinitializer, env);
{
walk_env *my_env = (walk_env *)env;
- if (get_entity_variability(ent) != variability_uninitialized) {
- if (ent->has_initializer) {
- walk_initializer(ent->attr.initializer, my_env);
- } else if (is_atomic_entity(ent)) {
- irg_walk(get_atomic_ent_value(ent), my_env->pre, my_env->post, my_env->env);
- } else {
- int i, n_vals = get_compound_ent_n_values(ent);
+ if (ent->initializer != NULL) {
+ walk_initializer(ent->initializer, my_env);
+ } else if (entity_has_compound_ent_values(ent)) {
+ int i, n_vals = get_compound_ent_n_values(ent);
- for (i = 0; i < n_vals; i++)
- irg_walk(get_compound_ent_value(ent, i), my_env->pre, my_env->post, my_env->env);
- }
+ for (i = 0; i < n_vals; i++)
+ irg_walk(get_compound_ent_value(ent, i), my_env->pre, my_env->post, my_env->env);
}
}
/* Walks over all code in const_code_irg. */
-void walk_const_code(irg_walk_func *pre, irg_walk_func *post, void *env) {
+void walk_const_code(irg_walk_func *pre, irg_walk_func *post, void *env)
+{
int i, j, n_types;
walk_env my_env;
+ ir_segment_t s;
ir_graph *rem = current_ir_graph;
current_ir_graph = get_const_code_irg();
my_env.env = env;
/* Walk all types that can contain constant entities. */
- walk_types_entities(get_glob_type(), &walk_entity, &my_env);
+ for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; s++)
+ walk_types_entities(get_segment_type(s), &walk_entity, &my_env);
n_types = get_irp_n_types();
for (i = 0; i < n_types; i++)
walk_types_entities(get_irp_type(i), &walk_entity, &my_env);