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