3d217fa1230f7d053cf041f031604b5a3b7349d5
[libfirm] / ir / ir / irgwalk.h
1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Author: Boris Boesler
5 **
6 ** traverse an ir graph
7 ** - execute the pre function before recursion
8 ** - execute the post function after recursion
9 **
10 ** Uses current_ir_graph (from irgraph.h)!!! Set it to the proper
11 ** graph before starting the walker.
12 */
13
14 /* $Id$ */
15
16 # ifndef _IRGWALK_H_
17 # define _IRGWALK_H_
18
19 # include "irnode.h"
20
21 /* type of callback function for ir_graph walk */
22 typedef void (* irg_walk_func)(ir_node *, void *);
23
24 /* Walks over the ir graph, starting at the node given as first argument.
25    Executes pre before visiting the predecessor of a node, post after.
26    irg_walk uses the visited flag in irg and the nodes to determine visited
27    nodes.  It executes inc_irg_visited(current_ir_graph) to generate a new
28    flag.  It marks the node as visited before executing pre.
29    The void* env can be used to pass status information between the
30    pre and post functions.  */
31 void irg_walk(ir_node *node, irg_walk_func pre, irg_walk_func post, void *env);
32
33 /* Like "irg_walk", but walks over all reachable nodes in the ir
34  * graph, starting at the end operation. During the walk current_ir_graph
35  * is set to irg. */
36 void irg_walk_graph(ir_graph *irg, irg_walk_func pre, irg_walk_func post, void *env);
37
38 /* Executes irg_walk(end, pre, post, env) for all irgraphs in irprog.
39    Sets current_ir_graph properly for each walk.  Conserves current
40    current_ir_graph. */
41 void all_irg_walk(irg_walk_func pre, irg_walk_func post, void *env);
42
43
44 /* Walks only over Block nodes in the graph.  Has it's own visited
45    flag, so that it can be interleaved with the other walker.
46    If a none block is passed, starts at the block this node belongs to.
47    If end is passed also visites kept alive blocks. */
48 void irg_block_walk(ir_node *node, irg_walk_func pre, irg_walk_func post, void *env);
49
50 /* Like "irg_block_walk", but walks over all reachable blocks in the
51  * ir graph, starting at the end block. */
52 void irg_block_walk_graph(ir_graph *irg, irg_walk_func pre, irg_walk_func post, void *env);
53
54 /* Walks over all code in const_code_irg.
55    Uses visited flag in const_code_irg. */
56 void walk_const_code(irg_walk_func pre, irg_walk_func post, void *env);
57
58 /********************************************************************/
59 /** Walking support for interprocedural analysis                   **/
60 /**                                                                **/
61 /** @@@ Don't use, not operational yet, doesn't grok recursions!!  **/
62 /**                                                                **/
63 /** Interprocedural walking should not walk all predecessors of    **/
64 /** all nodes.  When leaving a procedure the walker should only    **/
65 /** follow the edge corresponding to the most recent entry of the  **/
66 /** procedure.  The following functions use an internal stack to   **/
67 /** remember the current call site of a procedure.                 **/
68 /** They also set current_ir_graph correctly.                      **/
69 /**                                                                **/
70 /** Usage example:                                                 **/
71 /**                                                                **/
72 /** void init_ip_walk ();                                          **/
73 /** work_on_graph(some_end_node);                                  **/
74 /** void finish_ip_walk();                                         **/
75 /**                                                                **/
76 /** work_on_graph(ir_node *n) {                                    **/
77 /**   for (i = 0; i < get_irn_arity(n); i++) {                     **/
78 /**     if (...) continue;                                         **/
79 /**     ir_node *m = get_irn_ip_pred(n, i);                        **/
80 /**     if !m continue;                                            **/
81 /**     work_on_graph(m);                                          **/
82 /**     return_recur(n, i);                                        **/
83 /**   }                                                            **/
84 /** }                                                              **/
85 /********************************************************************/
86
87 /* Allocates some necessary datastructures. */
88 void init_ip_walk ();
89 /* Frees some necessary datastructures. */
90 void finish_ip_walk();
91
92 /* Call for i in {0|-1 ... get_irn_arity(n)}.
93    If n is a conventional node returns the same node as get_irn_n(n, i).
94    If the predecessors of n are in the callee of the procedure n belongs
95    to, returns get_irn_n(n, i) if this node is in the callee on the top
96    of the stack, else returns NULL.
97    If the predecessors of n are in a procedure called by the procedure n
98    belongs to pushes the caller on the caller stack in the callee.
99    Sets current_ir_graph to the graph the node returned is in. */
100 ir_node *get_irn_ip_pred(ir_node *n, int pos);
101
102 /* If get_irn_ip_pred() returned a node (not NULL) this must be
103    called to clear up the stacks.
104    Sets current_ir_graph to the graph n is in. */
105 void return_recur(ir_node *n, int pos);
106
107
108 # endif /* _IRGWALK_H_ */