faster irn_loop access
[libfirm] / ir / ana / irloop.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ana/irloop_t.h
4  * Purpose:     Loop datastructure and access functions.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:     7.2002
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2002-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14 * @file irloop.h
15 *
16 *  Computes backedges in the control and data flow.
17 *
18 *  @author Goetz Lindenmaier
19 *
20 *  Only Block and Phi/Filter nodes can have incoming backedges.
21 *  Constructs loops data structure: indicates loop nesting.
22 */
23
24 # ifndef _IRLOOP_H_
25 # define _IRLOOP_H_
26
27 # include "irgraph.h"
28 # include "irnode.h"
29
30
31 /* @@@ Interprocedural backedges ... ???? */
32
33 /*
34  * Backedge information.
35  *
36  * Predecessors of Block, Phi and interprocedural Filter nodes can
37  * have  backedges.  If loop information is computed, this
38  * information is computed, too.
39  * The backedge information can only be used if the graph is not in
40  * phase phase_building.
41  */
42
43 /** Returns true if the predesessor pos is a backedge. */
44 bool is_backedge (ir_node *n, int pos);
45 /** Remarks that edge pos is a backedge. */
46 void set_backedge (ir_node *n, int pos);
47 /** Remarks that edge pos is not a backedge. */
48 void set_not_backedge (ir_node *n, int pos);
49 /** Returns true if n has backedges. */
50 bool has_backedges (ir_node *n);
51 /** Sets backedge information to zero. */
52 void clear_backedges (ir_node *n);
53
54 /**
55  * The loops datastructure.
56  *
57  * The loops datastructure represents circles in the intermediate
58  * representation.  It does not represent loops in the terms of a
59  * source program.
60  * Each ir_graph can contain one outermost loop datastructure.
61  * loop is the entry point to the nested loops.
62  * The loop datastructure contains a field indicating the depth of
63  * the loop within the nesting.  Further it contains a list of the
64  * loops with nesting depth -1.  Finally it contains a list of all
65  * nodes in the loop.
66  *
67  * @todo We could add a field pointing from a node to the containing loop,
68  * this would cost a lot of memory, though.
69  */
70 typedef struct ir_loop ir_loop;
71
72 /* Loop elements are loop nodes and ir nodes */
73 typedef union {
74     firm_kind *kind;    /**< is either k_ir_node or k_ir_loop */
75     ir_node *node;      /**< Pointer to an ir_node element */
76     ir_loop *son;       /**< Pointer to an ir_loop element */
77 } loop_element;
78
79 void     set_irg_loop(ir_graph *irg, ir_loop *l);
80 ir_loop *get_irg_loop(ir_graph *irg);
81
82 /** Returns the loop n is contained in.  NULL if node is in no loop. */
83 ir_loop *get_irn_loop(ir_node *n);
84
85 /** Returns outer loop, itself if outermost. */
86 ir_loop *get_loop_outer_loop (ir_loop *loop);
87 /** Returns nesting depth of this loop */
88 int      get_loop_depth (ir_loop *loop);
89
90 /* Sons are the inner loops contained in this loop. */
91 /** Returns the number of inner loops */
92 int      get_loop_n_sons (ir_loop *loop);
93 ir_loop *get_loop_son (ir_loop *loop, int pos);
94 /** Returns the number of nodes contained in loop.  */
95 int      get_loop_n_nodes (ir_loop *loop);
96 ir_node *get_loop_node (ir_loop *loop, int pos);
97
98 /** Returns the number of elements contained in loop.  */
99 int      get_loop_n_elements (ir_loop *loop);
100 /** Returns a loop element.  A loop element can be interpreted as a
101     kind pointer, an ir_node* or an ir_loop*. */
102 loop_element get_loop_element (ir_loop *loop, int pos);
103
104 /*
105  * Constructing and destructing the loop/backedge information.
106  */
107
108 /** Constructs backedge information for irg in intraprocedural view. */
109 /* @@@ Well, maybe construct_loop_information or analyze_loops ? */
110 void construct_backedges(ir_graph *irg);
111
112 /** Constructs backedges for all irgs in interprocedural view.  All
113     loops in the graph will be marked as such, not only realizeable
114     loops and recursions in the program.  E.g., if the same funcion is
115     called twice, there is a loop between the first function return and
116     the second call.  */
117 void construct_ip_backedges(void);
118
119 /** Removes all loop information.
120     Resets all backedges */
121 void free_loop_information(ir_graph *irg);
122 void free_all_loop_information (void);
123
124 #endif /* _IRLOOP_H_ */