freeing of loop information added
[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.
83     assumes current_ir_graph set properly. */
84 /*  @@@ @@@ @@@ @@@@ @@@
85     current impl is very expensive: O(#nodes in irg).
86     Is used by heapanal (O(#phi)) --> better impl required. */
87 ir_loop *get_irn_loop(ir_node *n);
88
89 /** Returns outer loop, itself if outermost. */
90 ir_loop *get_loop_outer_loop (ir_loop *loop);
91 /** Returns nesting depth of this loop */
92 int      get_loop_depth (ir_loop *loop);
93
94 /* Sons are the inner loops contained in this loop. */
95 /** Returns the number of inner loops */
96 int      get_loop_n_sons (ir_loop *loop);
97 ir_loop *get_loop_son (ir_loop *loop, int pos);
98 /** Returns the number of nodes contained in loop.  */
99 int      get_loop_n_nodes (ir_loop *loop);
100 ir_node *get_loop_node (ir_loop *loop, int pos);
101
102 /** Returns the number of elements contained in loop.  */
103 int      get_loop_n_elements (ir_loop *loop);
104 /** Returns a loop element.  A loop element can be interpreted as a
105     kind pointer, an ir_node* or an ir_loop*. */
106 loop_element get_loop_element (ir_loop *loop, int pos);
107
108 /*
109  * Constructing and destructing the loop/backedge information.
110  */
111
112 /** Constructs backedge information for irg in intraprocedural view. */
113 /* @@@ Well, maybe construct_loop_information or analyze_loops ? */
114 void construct_backedges(ir_graph *irg);
115
116 /** Constructs backedges for all irgs in interprocedural view.  All
117     loops in the graph will be marked as such, not only realizeable
118     loops and recursions in the program.  E.g., if the same funcion is
119     called twice, there is a loop between the first function return and
120     the second call.  */
121 void construct_ip_backedges(void);
122
123 /** Removes all loop information.
124     Resets all backedges */
125 void free_loop_information(ir_graph *irg);
126 void free_all_loop_information (void);
127
128 #endif /* _IRLOOP_H_ */