add some const qualifiers to backedges query functions
[libfirm] / include / libfirm / irloop.h
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    Loop datastructure and access functions.
23  * @author   Goetz Lindenmaier
24  * @date     7.2002
25  * @version  $Id$
26  * @brief
27  *  Computes backedges in the control and data flow.
28  *
29  * @note
30  *  Only Block and Phi nodes can have incoming backedges.
31  *  Constructs loops data structure: indicates loop nesting.
32  */
33 #ifndef FIRM_ANA_IRLOOP_H
34 #define FIRM_ANA_IRLOOP_H
35
36 #include "firm_types.h"
37 #include "firm_common.h"
38 #include "begin.h"
39
40 /* ------------------------------------------------------------------- */
41 /*
42  * Backedge information.
43  *
44  * Predecessors of Block and Phi nodes can have  backedges.
45  * If loop information is computed, this information is computed, too.
46  * The backedge information can only be used if the graph is not in
47  * phase phase_building.
48  */
49 /* ------------------------------------------------------------------- */
50
51 /** Returns non-zero if the predecessor pos is a backedge. */
52 FIRM_API int is_backedge(const ir_node *n, int pos);
53 /** Marks edge pos as a backedge. */
54 FIRM_API void set_backedge(ir_node *n, int pos);
55 /** Marks edge pos as a non-backedge. */
56 FIRM_API void set_not_backedge(ir_node *n, int pos);
57 /** Returns non-zero if n has backedges. */
58 FIRM_API int has_backedges(const ir_node *n);
59 /** Clears all backedge information. */
60 FIRM_API void clear_backedges(ir_node *n);
61
62 /** Loop elements: loop nodes and ir nodes */
63 typedef union {
64         firm_kind *kind;    /**< is either k_ir_node or k_ir_loop */
65         ir_node  *node;     /**< Pointer to an ir_node element */
66         ir_loop  *son;      /**< Pointer to an ir_loop element */
67         ir_graph *irg;      /**< Pointer to an ir_graph element (only callgraph loop trees) */
68 } loop_element;
69
70 FIRM_API int is_ir_loop(const void *thing);
71
72 /** Set the outermost loop in ir graph as basic access to loop tree. */
73 FIRM_API void set_irg_loop(ir_graph *irg, ir_loop *l);
74
75 /* Returns the root loop info (if exists) for an irg. */
76 FIRM_API ir_loop *get_irg_loop(const ir_graph *irg);
77
78 /** Returns the loop n is contained in.  NULL if node is in no loop. */
79 FIRM_API ir_loop *get_irn_loop(const ir_node *n);
80
81 /** Returns outer loop, itself if outermost. */
82 FIRM_API ir_loop *get_loop_outer_loop(const ir_loop *loop);
83 /** Returns nesting depth of this loop */
84 FIRM_API unsigned get_loop_depth(const ir_loop *loop);
85
86 /* Sons are the inner loops contained in this loop. */
87 /** Returns the number of inner loops */
88 FIRM_API size_t get_loop_n_sons(const ir_loop *loop);
89
90 /** Returns the pos`th son loop (inner loop) of a loop.
91 Returns NULL if there is not a pos`th loop_node. */
92 FIRM_API ir_loop *get_loop_son(ir_loop *loop, size_t pos);
93
94 /** Returns the number of nodes contained in loop.  */
95 FIRM_API size_t get_loop_n_nodes(const ir_loop *loop);
96
97 /** Returns the pos`th ir_node of a loop.
98 Returns NULL if there is not a pos`th ir_node. */
99 FIRM_API ir_node *get_loop_node(const ir_loop *loop, size_t pos);
100
101 /** Returns the number of elements contained in loop.  */
102 FIRM_API size_t get_loop_n_elements(const ir_loop *loop);
103
104 /** Returns a loop element.  A loop element can be interpreted as a
105 kind pointer, an ir_node* or an ir_loop*. */
106 FIRM_API loop_element get_loop_element(const ir_loop *loop, size_t pos);
107
108 #define INVALID_LOOP_POS ((size_t)-1)
109
110 /** Returns the element number of the loop son in loop.
111 *  Returns INVALID_LOOP_POS if not found. O(|elements|). */
112 FIRM_API size_t get_loop_element_pos(const ir_loop *loop, void *le);
113
114 /** Returns a unique node number for the loop node to make output
115 readable. If libfirm_debug is not set it returns the loop cast to
116 int. */
117 FIRM_API long get_loop_loop_nr(const ir_loop *loop);
118
119 /** A field to connect additional information to a loop. */
120 FIRM_API void set_loop_link(ir_loop *loop, void *link);
121 FIRM_API void *get_loop_link(const ir_loop *loop);
122
123 /* ------------------------------------------------------------------- */
124 /* Constructing and destructing the loop/backedge information.         */
125 /* ------------------------------------------------------------------- */
126
127 /** Constructs backedge information and loop tree for a graph.
128  *
129  *  The algorithm views the program representation as a pure graph.
130  *  It assumes that only block and phi nodes may be loop headers.
131  *  The resulting loop tree is a possible visiting order for dataflow
132  *  analysis.
133  *
134  *  This algorithm destoyes the link field of block nodes.
135  *
136  *  @returns Maximal depth of loop tree.
137  *
138  *  @remark
139  *  One assumes, the Phi nodes in a block with a backedge have backedges
140  *  at the same positions as the block.  This is not the case, as
141  *  the scc algorithms does not respect the program semantics in this case.
142  *  Take a swap in a loop (t = i; i = j; j = t;)  This results in two Phi
143  *  nodes.  They form a cycle.  Once the scc algorithm deleted one of the
144  *  edges, the cycle is removed.  The second Phi node does not get a
145  *  backedge!
146  */
147 FIRM_API int construct_backedges(ir_graph *irg);
148
149 /**
150  * Construct Intra-procedural control flow loop tree for a IR-graph.
151  *
152  * This constructs loop information resembling the program structure.
153  * It is useful for loop optimizations and analyses, as, e.g., finding
154  * iteration variables or loop invariant code motion.
155  *
156  * This algorithm computes only back edge information for Block nodes, not
157  * for Phi nodes.
158  *
159  * This algorithm destroyes the link field of block nodes.
160  *
161  * @param irg  the graph
162  *
163  * @returns Maximal depth of loop tree.
164  */
165 FIRM_API int construct_cf_backedges(ir_graph *irg);
166
167 /**
168  * Computes Intra-procedural control flow loop tree on demand.
169  *
170  * @param irg  the graph
171  */
172 FIRM_API void assure_cf_loop(ir_graph *irg);
173
174 /**
175  * Removes all loop information.
176  * Resets all backedges.  Works for any construction algorithm.
177  */
178 FIRM_API void free_loop_information(ir_graph *irg);
179 FIRM_API void free_all_loop_information (void);
180
181
182 /* ------------------------------------------------------------------- */
183 /* Simple analyses based on the loop information                       */
184 /* ------------------------------------------------------------------- */
185
186 /** Test whether a value is loop invariant.
187  *
188  * @param n      The node to be tested.
189  * @param block  A block node.
190  *
191  * Returns non-zero, if the node n is not changed in the loop block
192  * belongs to or in inner loops of this block. */
193 FIRM_API int is_loop_invariant(const ir_node *n, const ir_node *block);
194
195 #include "end.h"
196
197 #endif