# include <config.h>
#endif
-# include <stdlib.h>
+#include <stdlib.h>
-# include "irnode_t.h"
-# include "irgraph_t.h" /* visited flag */
-# include "irprog.h"
-# include "irgwalk.h"
-# include "typewalk.h"
-# include "firmstat.h"
-# include "ircgcons.h"
+#include "irnode_t.h"
+#include "irgraph_t.h" /* visited flag */
+#include "irprog.h"
+#include "irgwalk.h"
+#include "typewalk.h"
+#include "firmstat.h"
+#include "ircgcons.h"
-# include "eset.h"
-# include "array.h"
+#include "eset.h"
+#include "array.h"
/* walk over an interprocedural graph (callgraph). Visits only graphs in irg_set. */
static void irg_walk_cg(ir_node * node, int visited, eset * irg_set,
entity * ent = get_Call_callee(node, i);
ir_graph * irg = ent ? get_entity_irg(ent) : NULL;
if (irg && !eset_contains(irg_set, irg)) {
- eset_insert(irg_set, irg);
- irg_walk_graph(irg, (irg_walk_func *) collect_irgs, NULL, irg_set);
+ eset_insert(irg_set, irg);
+ irg_walk_graph(irg, (irg_walk_func *) collect_irgs, NULL, irg_set);
}
}
}
}
+/**
+ * specialized version of irg_walk_2, called if only pre callback exists
+ */
static void
-irg_walk_2(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void * env)
-{
+irg_walk_2_pre(ir_node *node, irg_walk_func *pre, void * env) {
int i;
- assert(node && node->kind==k_ir_node);
-#if 0 /* safe */
- if (get_irn_visited(node) < get_irg_visited(current_ir_graph)) {
- set_irn_visited(node, get_irg_visited(current_ir_graph));
+ set_irn_visited(node, current_ir_graph->visited);
- if (pre) pre(node, env);
+ pre(node, env);
- if (is_no_Block(node))
- irg_walk_2(get_nodes_block(node), pre, post, env);
- for (i = get_irn_arity(node) - 1; i >= 0; --i)
- irg_walk_2(get_irn_n(node, i), pre, post, env);
+ if (node->op != op_Block) {
+ ir_node *pred = get_irn_n(node, -1);
+ if (pred->visited < current_ir_graph->visited)
+ irg_walk_2_pre(pred, pre, env);
+ }
+ for (i = get_irn_arity(node) - 1; i >= 0; --i) {
+ ir_node *pred = get_irn_n(node, i);
+ if (pred->visited < current_ir_graph->visited)
+ irg_walk_2_pre(pred, pre, env);
+ }
+}
- if (post) post(node, env);
+/**
+ * specialized version of irg_walk_2, called if only post callback exists
+ */
+static void
+irg_walk_2_post(ir_node *node, irg_walk_func *post, void * env) {
+ int i;
+ set_irn_visited(node, current_ir_graph->visited);
+
+ if (node->op != op_Block) {
+ ir_node *pred = get_irn_n(node, -1);
+ if (pred->visited < current_ir_graph->visited)
+ irg_walk_2_post(pred, post, env);
+ }
+ for (i = get_irn_arity(node) - 1; i >= 0; --i) {
+ ir_node *pred = get_irn_n(node, i);
+ if (pred->visited < current_ir_graph->visited)
+ irg_walk_2_post(pred, post, env);
}
-#else /* faster */
- if (node->visited < current_ir_graph->visited) {
- set_irn_visited(node, current_ir_graph->visited);
- if (pre) pre(node, env);
+ post(node, env);
+}
- if (node->op != op_Block)
- irg_walk_2(get_irn_n(node, -1), pre, post, env);
- for (i = get_irn_arity(node) - 1; i >= 0; --i)
- irg_walk_2(get_irn_n(node, i), pre, post, env);
+/**
+ * specialized version of irg_walk_2, called if pre and post callbacks exist
+ */
+static void
+irg_walk_2_both(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void * env) {
+ int i;
+ set_irn_visited(node, current_ir_graph->visited);
- if (post) post(node, env);
+ pre(node, env);
+
+ if (node->op != op_Block) {
+ ir_node *pred = get_irn_n(node, -1);
+ if (pred->visited < current_ir_graph->visited)
+ irg_walk_2_both(pred, pre, post, env);
}
-#endif
+ for (i = get_irn_arity(node) - 1; i >= 0; --i) {
+ ir_node *pred = get_irn_n(node, i);
+ if (pred->visited < current_ir_graph->visited)
+ irg_walk_2_both(pred, pre, post, env);
+ }
+
+ post(node, env);
}
+/**
+ * Intraprozedural graph walker.
+ */
+static void
+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) irg_walk_2_pre (node, pre, env);
+ else if (!pre) irg_walk_2_post(node, post, env);
+ else irg_walk_2_both(node, pre, post, env);
+ }
+}
+/*
+ * generic graph walker
+ */
void irg_walk(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void *env)
{
assert(node && node->kind==k_ir_node);
static ir_node *get_cf_op(ir_node *n) {
ir_node *pred;
- n = skip_nop(n);
+ n = skip_Id(n);
n = skip_Tuple(n);
pred = skip_Proj(n);
if (!(is_cfop(pred) || is_fragile_op(pred) ||
for (i = 0; i < arity; i++) {
pred = get_irn_n(node, i);
if (get_irn_op(pred) == op_Block)
- irg_block_walk_2(pred, pre, post, env);
+ irg_block_walk_2(pred, pre, post, env);
}
}
int i, n = get_compound_ent_n_values(ent);
for (i = 0; i < n; i++)
- irg_walk(get_compound_ent_value(ent, i), my_env->pre, my_env->post, my_env->env);
+ irg_walk(get_compound_ent_value(ent, i), my_env->pre, my_env->post, my_env->env);
}
}
}
current_ir_graph = rem;
}
-
-
-/********************************************************************/
-/** Walking support for interprocedural analysis **/
-/** **/
-/** @@@ Don't use, not operational yet, doesn't grok recursions!! **/
-/** @@@ Header for irgwalk.h, here until it works. **/
-/** **/
-/** Interprocedural walking should not walk all predecessors of **/
-/** all nodes. When leaving a procedure the walker should only **/
-/** follow the edge corresponding to the most recent entry of the **/
-/** procedure. The following functions use an internal stack to **/
-/** remember the current call site of a procedure. **/
-/** They also set current_ir_graph correctly. **/
-/** **/
-/** Usage example: **/
-/** **/
-/** void init_ip_walk (); **/
-/** work_on_graph(some_end_node); **/
-/** void finish_ip_walk(); **/
-/** **/
-/** work_on_graph(ir_node *n) { **/
-/** for (i = 0; i < get_irn_arity(n); i++) { **/
-/** if (...) continue; **/
-/** ir_node *m = get_irn_ip_pred(n, i); **/
-/** if !m continue; **/
-/** work_on_graph(m); **/
-/** return_recur(n, i); **/
-/** } **/
-/** } **/
-/********************************************************************/
-
-/* Call for i in {0|-1 ... get_irn_arity(n)}.
- If n is a conventional node returns the same node as get_irn_n(n, i).
- If the predecessors of n are in the callee of the procedure n belongs
- to, returns get_irn_n(n, i) if this node is in the callee on the top
- of the stack, else returns NULL.
- If the predecessors of n are in a procedure called by the procedure n
- belongs to pushes the caller on the caller stack in the callee.
- Sets current_ir_graph to the graph the node returned is in. */
-ir_node *get_irn_ip_pred(ir_node *n, int pos);
-
-/* If get_irn_ip_pred() returned a node (not NULL) this must be
- called to clear up the stacks.
- Sets current_ir_graph to the graph n is in. */
-void return_recur(ir_node *n, int pos);
-
-
-/********************************************************************/
-/** Walking support for interprocedural analysis **/
-/********************************************************************/
-
-#define MIN_STACK_SIZE 40
-
-typedef struct callsite_stack {
- int tos;
- ir_node **s;
-} callsite_stack;
-
-/* Access the stack in the irg **************************************/
-
-static INLINE void
-set_irg_callsite_stack(ir_graph *g, callsite_stack *s) {
- set_irg_link(g, s);
-}
-
-static INLINE callsite_stack *
-get_irg_callsite_stack(ir_graph *g) {
- return (callsite_stack *) get_irg_link(g);
-}
-
-/* A stack of callsites *********************************************/
-
-/* @@@ eventually change the implementation so the new_ only sets the field
- to NULL, and the stack is only allocated if used. Saves Memory! */
-static INLINE callsite_stack *
-new_callsite_stack(ir_graph *g) {
- callsite_stack *res = (callsite_stack *)malloc(sizeof(callsite_stack));
- res->tos = 0;
- res->s = NEW_ARR_F (ir_node *, MIN_STACK_SIZE);
- set_irg_callsite_stack(g, res);
- return(res);
-}
-
-static INLINE void
-free_callsite_stack(ir_graph *g) {
- callsite_stack *s = get_irg_callsite_stack(g);
- DEL_ARR_F(s->s);
- free(s);
-}
-
-static INLINE void
-push_callsite(ir_graph *callee, ir_node *callsite) {
- callsite_stack *s = get_irg_callsite_stack(callee);
- if (s->tos == ARR_LEN(s->s)) {
- int nlen = ARR_LEN (s->s) * 2;
- ARR_RESIZE (ir_node *, s->s, nlen);
- }
- s->s[s->tos] = callsite;
- s->tos++;
-}
-
-static INLINE ir_node *
-get_top_of_callsite_stack(ir_graph *callee) {
- callsite_stack *s = get_irg_callsite_stack(callee);
- return (s->s[s->tos-1]);
-}
-
-static INLINE
-ir_node * pop_callsite(ir_graph *callee) {
- callsite_stack *s = get_irg_callsite_stack(callee);
- s->tos--;
- return (s->s[s->tos]);
-}
-
-
-/* Initialization routines ******************************************/
-
-void init_ip_walk (void) {
- int i;
- for (i = 0; i < get_irp_n_irgs(); i++)
- new_callsite_stack(get_irp_irg(i));
-}
-
-void finish_ip_walk(void) {
- int i;
- for (i = 0; i < get_irp_n_irgs(); i++)
- free_callsite_stack(get_irp_irg(i));
- set_irg_link(get_irp_irg(i), NULL);
-}
-
-/* walker routines **************************************************/
-
-/* cf_pred is End* */
-static INLINE void
-enter_procedure(ir_node *block, ir_node *cf_pred, int pos) {
- ir_node *callbegin;
- ir_graph *irg = get_irn_irg(cf_pred);
-
- assert(interprocedural_view);
-
- interprocedural_view = 0;
- callbegin = skip_Proj(get_irn_n(block, 0));
- assert(get_irn_op(callbegin) == op_CallBegin);
- interprocedural_view = 1;
-
- push_callsite(irg, callbegin);
- current_ir_graph = irg;
-}
-
-/* cf_pred is CallBegin */
-static INLINE bool
-leave_procedure(ir_node *block, ir_node *cf_pred, int pos) {
- ir_node *tos = get_top_of_callsite_stack(current_ir_graph);
-
- assert(get_irn_op(cf_pred) == op_CallBegin);
-
- if (tos == cf_pred) {
- /* We entered this procedure by the call pred pos refers to. */
- pop_callsite(current_ir_graph);
- current_ir_graph = get_CallBegin_irg(cf_pred);
- return true;
- } else {
- /* We won't walk. */
- return false;
- }
-}
-
-ir_node *get_irn_ip_pred(ir_node *n, int pos) {
-
- if (interprocedural_view) {
-
- /* Find the cf_pred refering to pos. */
- ir_node *block = n;
- ir_node *cf_pred;
- if (get_irn_opcode(n) == iro_Filter) block = get_nodes_block(n);
- cf_pred = skip_Proj(get_irn_n(block, pos));
-
- /* Check whether we enter or leave a procedure and act according. */
- if ((get_irn_op(cf_pred) == op_EndReg) ||
- (get_irn_op(cf_pred) == op_EndExcept))
- enter_procedure(block, cf_pred, pos);
- if (get_irn_op(cf_pred) == op_CallBegin)
- if (!leave_procedure(block, cf_pred, pos)) return NULL;
- }
-
- return get_irn_n(n, pos);
-}
-
-static INLINE void
-re_enter_procedure(ir_node *block, ir_node *cf_pred, int pos) {
- ir_node *callbegin = pop_callsite(current_ir_graph);
- assert(interprocedural_view);
- current_ir_graph = get_CallBegin_irg(callbegin);
-}
-
-static INLINE void
-re_leave_procedure(ir_node *block, ir_node *cf_pred, int pos) {
- ir_node *proj;
- ir_graph *callee;
-
- assert(get_irn_op(cf_pred) == op_CallBegin);
-
- /* Find the irg block is in. */
- proj = get_Block_cg_cfgpred(block, pos);
- assert(is_Proj(proj));
- callee = get_entity_irg(get_Call_callee(get_CallBegin_call(cf_pred),
- get_Proj_proj(proj)));
- current_ir_graph = callee;
- push_callsite(callee, cf_pred);
-}
-
-void
-return_recur(ir_node *n, int pos) {
- ir_node *block;
- ir_node *cf_pred;
-
- if (!interprocedural_view) return;
-
- /* Find the cf_pred refering to pos. */
- block = n;
- if (get_irn_opcode(n) == iro_Filter) block = get_nodes_block(n);
- cf_pred = skip_Proj(get_irn_n(block, pos));
-
- /* Check whether we re_enter or re_leave a procedure and act according. */
- if ((get_irn_op(cf_pred) == op_EndReg) ||
- (get_irn_op(cf_pred) == op_EndExcept))
- re_enter_procedure(block, cf_pred, pos);
- if (get_irn_op(cf_pred) == op_CallBegin)
- re_leave_procedure(block, cf_pred, pos);
-}