irmemwalk.c 6.64 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* -*- c -*- */

/*
 * Project:     libFIRM
 * File name:   ir/ana2/irmemwalk.c
 * Purpose:     walk along memory edges
 * Author:      Florian
 * Modified by:
 * Created:     Mon 18 Oct 2004
 * CVS-ID:      $Id$
 * Copyright:   (c) 1999-2004 Universitt Karlsruhe
 * Licence:     This file is protected by GPL -  GNU GENERAL PUBLIC LICENSE.
 */

/*
  Walk over a firm graph along its memory edges.

  Any number of graphs can be visited at the same time, but no graph
  can be traversed more than once at any time.
*/


# ifdef HAVE_CONFIG_H
Michael Beck's avatar
Michael Beck committed
24
#  include "config.h"
25
26
# endif

Michael Beck's avatar
Michael Beck committed
27
# include "irnode_t.h"
28
# include "irgwalk.h"           /* for irg_walk_func */
Florian Liekweg's avatar
Florian Liekweg committed
29
# include "irprog.h"            /* for get_irp_main_irg */
30
31
32
33
34
35
36
37
38
39
40
# include "xmalloc.h"

# ifndef TRUE
#  define TRUE 1
#  define FALSE 0
# endif /* not defined TRUE */

/*
   Data
*/

Michael Beck's avatar
Michael Beck committed
41
/** environment for a single memory walker */
42
typedef struct walk_mem_env_str {
Michael Beck's avatar
Michael Beck committed
43
  ir_graph *graph;              /**< the graph we're visiting */
44
45
  unsigned long visited;        /**< 'visited' marker
                                 (unsigned long in case we walk more than 2^32 graphs) */
Michael Beck's avatar
Michael Beck committed
46
47
48
  irg_walk_func *pre;           /**< pre action */
  irg_walk_func *post;          /**< post action */
  void *env;                    /**< user-defined environment */
49

Michael Beck's avatar
Michael Beck committed
50
  struct walk_mem_env_str *prev; /**< link up walking instances */
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  /* what else? */
} walk_mem_env_t;

/*
  Globals
*/

/* Link up walking instances */
static walk_mem_env_t *walk_envs = NULL;

/*
  Walk over the firm nodes of a graph via the memory edges (only)
  starting from a node that has a memory input.
*/
static void irg_walk_mem_node (ir_node *node,
                               walk_mem_env_t *walk_env)
{
  const opcode op = get_irn_opcode (node);
  ir_node *in = NULL;

  if (get_irn_visited (node) >= walk_env->visited) {
    return;
  } else {
74
    set_irn_visited (node, walk_env->visited);
75
76
  }

Florian Liekweg's avatar
Florian Liekweg committed
77
78
79
80
81
  if (op_NoMem == get_irn_op (node)) {
    /* We don't want to see it it if it's not memory */
    return;
  }

82
83
84
85
86
87
88
89
90
91
  if (iro_Proj == op) {
    /* We don't want to see proj nodes at all --- skip over them */
    in = get_Proj_pred (node);

    irg_walk_mem_node (in, walk_env);

    return;
  }

  /* execute the 'pre' function */
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  if (NULL != walk_env->pre) {
    walk_env->pre (node, walk_env->env);
  }

  switch (op) {
  case (iro_Start): {
  } break;
  case (iro_Load): {
    in = get_Load_mem (node);

    irg_walk_mem_node (in, walk_env);
  } break;
  case (iro_Store): {
    in = get_Store_mem (node);

    irg_walk_mem_node (in, walk_env);
  } break;
  case (iro_Alloc): {
    in = get_Alloc_mem (node);

    irg_walk_mem_node (in, walk_env);
  } break;
  case (iro_Free): {
    in = get_Free_mem (node);
    /* WTF? */
    irg_walk_mem_node (in, walk_env);
  } break;
  case (iro_Raise): {
    in = get_Raise_mem (node);

    irg_walk_mem_node (in, walk_env);
  } break;
  case (iro_Sel): {
    in = get_Sel_mem (node);

    irg_walk_mem_node (in, walk_env);
  } break;
  case (iro_Call): {
    in = get_Call_mem (node);

    irg_walk_mem_node (in, walk_env);
  } break;
  case (iro_Return): {
    in = get_Return_mem (node);

    irg_walk_mem_node (in, walk_env);
  } break;
  case (iro_Phi): {
    int i;
    int n_ins = get_irn_arity (node);

    for (i = 0; i < n_ins; i ++) {
      in = get_irn_n (node, i);

      irg_walk_mem_node (in, walk_env);
    }
  } break;
  case (iro_Div): {
    in = get_Div_mem (node);

    irg_walk_mem_node (in, walk_env);
  } break;
  case (iro_Quot): {
    in = get_Quot_mem (node);

    irg_walk_mem_node (in, walk_env);
  } break;
  case (iro_Mod): {
    in = get_Mod_mem (node);

    irg_walk_mem_node (in, walk_env);
  } break;
  case (iro_DivMod): {
    in = get_DivMod_mem (node);

    irg_walk_mem_node (in, walk_env);
  } break;
Florian Liekweg's avatar
Florian Liekweg committed
169
170
171
172
173
174
175
176
177
178
179
  case (iro_Block): {
    /* End Block ONLY */
    int i;
    int n_ins = get_irn_arity (node);

    for (i = 0; i < n_ins; i ++) {
      ir_node *ret = get_irn_n (node, i);

      irg_walk_mem_node (ret, walk_env);
    }
  } break;
180
  default: {
181
182
    fprintf (stderr, "%s: not handled: node[%li].op = %s\n",
             __FUNCTION__,
183
184
185
             get_irn_node_nr (node),
             get_op_name (get_irn_op (node)));

186
187
188
189
    assert (0 && "something not handled");
  }
  }

190
  /* execute the 'post' function */
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
  if (NULL != walk_env->post) {
    walk_env->post (node, walk_env->env);
  }
}

/*
   See whether the given graph is being visited right now.
   We can't be visiting a graph multiple times.
*/
int get_irg_is_mem_visited (ir_graph *graph)
{
  walk_mem_env_t *walk_env = walk_envs;

  while (NULL != walk_env) {
    if (graph == walk_env->graph) {
      return (TRUE);
    }

    walk_env = walk_env->prev;
  }

  return (FALSE);
}

/*
  Walk over the nodes of the given graph via the memory edges (only).
  Each graph can only be subject to this walk once at any given time.
*/
void irg_walk_mem (ir_graph *graph,
                   irg_walk_func *pre, irg_walk_func *post,
                   void *env)
{
223
  ir_node *end_block = get_irg_end_block (graph);
224
  walk_mem_env_t *walk_env = (walk_mem_env_t*) xmalloc (sizeof (walk_mem_env_t));
225
226
227
228
229
230
231
232
233
234
235
236
237
238

  assert (! get_irg_is_mem_visited (graph));

  walk_env->graph = graph;
  inc_irg_visited (walk_env->graph);
  walk_env->visited = get_irg_visited (graph);

  walk_env->prev = walk_envs;
  walk_envs = walk_env;

  walk_env->pre = pre;
  walk_env->post = post;
  walk_env->env  = env;

239
  /* 'graph' is not actually being visited right now, so make sure it is reported that way */
240
241
  assert (get_irg_is_mem_visited (graph));

242
243
244
245
246
247
248
  /*
    The ins of the end BLOCK are either 'return's (regular exits) or
    'ProjX'/'Raise's (exception exits).  We only walk over the
    'return' nodes, assuming that all memory-changing nodes are found
    from there on.
  */
  irg_walk_mem_node (end_block, walk_env);
249
250
251
252
253
254
255
256
257
258
259
260
  /*
    The end NODE sometimes has some more ins. not sure whether we need to walk them.
  */

  /* allow only properly nested calls right now */
  assert (walk_envs == walk_env);
  walk_envs = walk_envs->prev;

  free (walk_env);

  assert (! get_irg_is_mem_visited (graph));
}
261
262
263
264
265



/*
  $Log$
266
267
268
  Revision 1.9  2005/01/10 17:26:34  liekweg
  fixup printfs, don't put environments on the stack

Michael Beck's avatar
Michael Beck committed
269
270
271
  Revision 1.8  2004/12/22 14:43:14  beck
  made allocations C-like

Michael Beck's avatar
Michael Beck committed
272
273
274
275
  Revision 1.7  2004/12/21 14:25:35  beck
  removed C99 constructs
  make visit counter of same type as irn visit counter

Michael Beck's avatar
Michael Beck committed
276
277
278
  Revision 1.6  2004/12/02 16:17:51  beck
  fixed config.h include

Florian Liekweg's avatar
Florian Liekweg committed
279
280
281
  Revision 1.5  2004/11/19 10:35:20  liekweg
  also test for NoMem

282
283
284
  Revision 1.4  2004/11/18 16:35:11  liekweg
  Do not touch Proj nodes at all

Florian Liekweg's avatar
Florian Liekweg committed
285
286
287
  Revision 1.3  2004/11/04 14:57:12  liekweg
  fixed end block handling

288
289
290
291
292
  Revision 1.2  2004/10/22 14:41:12  liekweg
  execute 'pre' for a change.  Also, add CVS log


*/