properly mark symbols in the public API to be exported. This allows us to use -fvisib...
[libfirm] / include / libfirm / irloop.h
1 /*
2  * Copyright (C) 1995-2008 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/Filter 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, Phi and interprocedural Filter nodes can
45  * have  backedges.  If loop information is computed, this
46  * information is computed, too.
47  * The backedge information can only be used if the graph is not in
48  * phase phase_building.
49  */
50 /* ------------------------------------------------------------------- */
51
52 #ifdef INTERPROCEDURAL_VIEW
53 /** Returns true if the predecessor pos is a backedge in the interprocedural view. */
54 FIRM_DLL int is_inter_backedge(ir_node *n, int pos);
55 /** Returns true if the predecessor pos is a backedge in the intraprocedural view. */
56 FIRM_DLL int is_intra_backedge(ir_node *n, int pos);
57 #endif
58 /** Returns non-zero if the predecessor pos is a backedge. */
59 FIRM_DLL int is_backedge(ir_node *n, int pos);
60 /** Marks edge pos as a backedge. */
61 FIRM_DLL void set_backedge(ir_node *n, int pos);
62 /** Marks edge pos as a non-backedge. */
63 FIRM_DLL void set_not_backedge(ir_node *n, int pos);
64 /** Returns non-zero if n has backedges. */
65 FIRM_DLL int has_backedges(ir_node *n);
66 /** Clears all backedge information. */
67 FIRM_DLL void clear_backedges(ir_node *n);
68
69 /** Loop elements: loop nodes and ir nodes */
70 typedef union {
71         firm_kind *kind;    /**< is either k_ir_node or k_ir_loop */
72         ir_node  *node;     /**< Pointer to an ir_node element */
73         ir_loop  *son;      /**< Pointer to an ir_loop element */
74         ir_graph *irg;      /**< Pointer to an ir_graph element (only callgraph loop trees) */
75 } loop_element;
76
77 FIRM_DLL int is_ir_loop(const void *thing);
78
79 /** Set the outermost loop in ir graph as basic access to loop tree. */
80 FIRM_DLL void set_irg_loop(ir_graph *irg, ir_loop *l);
81
82 /* Returns the root loop info (if exists) for an irg. */
83 FIRM_DLL ir_loop *get_irg_loop(const ir_graph *irg);
84
85 /** Returns the loop n is contained in.  NULL if node is in no loop. */
86 FIRM_DLL ir_loop *get_irn_loop(const ir_node *n);
87
88 /** Returns outer loop, itself if outermost. */
89 FIRM_DLL ir_loop *get_loop_outer_loop(const ir_loop *loop);
90 /** Returns nesting depth of this loop */
91 FIRM_DLL int get_loop_depth(const ir_loop *loop);
92
93 /* Sons are the inner loops contained in this loop. */
94 /** Returns the number of inner loops */
95 FIRM_DLL int get_loop_n_sons(const ir_loop *loop);
96
97 /** Returns the pos`th son loop (inner loop) of a loop.
98 Returns NULL if there is not a pos`th loop_node. */
99 FIRM_DLL ir_loop *get_loop_son(ir_loop *loop, int pos);
100
101 /** Returns the number of nodes contained in loop.  */
102 FIRM_DLL int get_loop_n_nodes(const ir_loop *loop);
103
104 /** Returns the pos`th ir_node of a loop.
105 Returns NULL if there is not a pos`th ir_node. */
106 FIRM_DLL ir_node *get_loop_node(const ir_loop *loop, int pos);
107
108 /** Returns the number of elements contained in loop.  */
109 FIRM_DLL int get_loop_n_elements(const ir_loop *loop);
110
111 /** Returns a loop element.  A loop element can be interpreted as a
112 kind pointer, an ir_node* or an ir_loop*. */
113 FIRM_DLL loop_element get_loop_element(const ir_loop *loop, int pos);
114
115 /** Returns the element number of the loop son in loop.
116 *  Returns -1 if not found. O(|elements|). */
117 FIRM_DLL int get_loop_element_pos(const ir_loop *loop, void *le);
118
119 /** Returns a unique node number for the loop node to make output
120 readable. If libfirm_debug is not set it returns the loop cast to
121 int. */
122 FIRM_DLL int get_loop_loop_nr(const ir_loop *loop);
123
124 /** A field to connect additional information to a loop. */
125 FIRM_DLL void set_loop_link(ir_loop *loop, void *link);
126 FIRM_DLL void *get_loop_link(const ir_loop *loop);
127
128 /* ------------------------------------------------------------------- */
129 /* Constructing and destructing the loop/backedge information.         */
130 /* ------------------------------------------------------------------- */
131
132 /** Constructs backedge information and loop tree for a graph in intraprocedural view.
133  *
134  *  The algorithm views the program representation as a pure graph.
135  *  It assumes that only block and phi nodes may be loop headers.
136  *  The resulting loop tree is a possible visiting order for dataflow
137  *  analysis.
138  *
139  *  This algorithm destoyes the link field of block nodes.
140  *
141  *  @returns Maximal depth of loop tree.
142  *
143  *  @remark
144  *  One assumes, the Phi nodes in a block with a backedge have backedges
145  *  at the same positions as the block.  This is not the case, as
146  *  the scc algorithms does not respect the program semantics in this case.
147  *  Take a swap in a loop (t = i; i = j; j = t;)  This results in two Phi
148  *  nodes.  They form a cycle.  Once the scc algorithm deleted one of the
149  *  edges, the cycle is removed.  The second Phi node does not get a
150  *  backedge!
151  */
152 FIRM_DLL int construct_backedges(ir_graph *irg);
153
154 #ifdef INTERPROCEDURAL_VIEW
155 /** Constructs backedges for all irgs in interprocedural view.
156  *
157  *  @see As construct_backedges(), but for interprocedural view.
158  *
159  *  @remark
160  *  All loops in the graph will be marked as such, not only
161  *  realizeable loops and recursions in the program.  E.g., if the
162  *  same funcion is called twice, there is a loop between the first
163  *  function return and the second call.
164  *
165  *  @returns Maximal depth of loop tree.
166  */
167 FIRM_DLL int construct_ip_backedges(void);
168 #endif
169
170 /**
171  * Construct Intra-procedural control flow loop tree for a IR-graph.
172  *
173  * This constructs loop information resembling the program structure.
174  * It is useful for loop optimizations and analyses, as, e.g., finding
175  * iteration variables or loop invariant code motion.
176  *
177  * This algorithm computes only back edge information for Block nodes, not
178  * for Phi nodes.
179  *
180  * This algorithm destroyes the link field of block nodes.
181  *
182  * @param irg  the graph
183  *
184  * @returns Maximal depth of loop tree.
185  */
186 FIRM_DLL int construct_cf_backedges(ir_graph *irg);
187
188 /**
189  * Computes Intra-procedural control flow loop tree on demand.
190  *
191  * @param irg  the graph
192  */
193 FIRM_DLL void assure_cf_loop(ir_graph *irg);
194
195 #ifdef INTERPROCEDURAL_VIEW
196 /**
197  * Construct Inter-procedural control flow loop tree.
198  *
199  * @see construct_cf_backedges() and construct_ip_backedges().
200  */
201 FIRM_DLL int construct_ip_cf_backedges(void);
202 #endif
203
204 /**
205  * Removes all loop information.
206  * Resets all backedges.  Works for any construction algorithm.
207  */
208 FIRM_DLL void free_loop_information(ir_graph *irg);
209 FIRM_DLL void free_all_loop_information (void);
210
211
212 /* ------------------------------------------------------------------- */
213 /* Simple analyses based on the loop information                       */
214 /* ------------------------------------------------------------------- */
215
216 /** Test whether a value is loop invariant.
217  *
218  * @param n      The node to be tested.
219  * @param block  A block node.
220  *
221  * Returns non-zero, if the node n is not changed in the loop block
222  * belongs to or in inner loops of this block. */
223 FIRM_DLL int is_loop_invariant(const ir_node *n, const ir_node *block);
224
225 #include "end.h"
226
227 #endif