2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Loop datastructure and access functions.
23 * @author Goetz Lindenmaier
27 * Computes backedges in the control and data flow.
30 * Only Block and Phi/Filter nodes can have incoming backedges.
31 * Constructs loops data structure: indicates loop nesting.
33 #ifndef FIRM_ANA_IRLOOP_H
34 #define FIRM_ANA_IRLOOP_H
36 #include "firm_types.h"
38 /* ------------------------------------------------------------------- */
40 * Backedge information.
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.
48 /* ------------------------------------------------------------------- */
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);
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);
67 /** Loop elements: loop nodes and ir nodes */
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 */
74 int is_ir_loop(const void *thing);
76 /** Set the outermost loop in ir graph as basic access to loop tree. */
77 void set_irg_loop(ir_graph *irg, ir_loop *l);
79 /* Returns the root loop info (if exists) for an irg. */
80 ir_loop *get_irg_loop(ir_graph *irg);
82 /** Returns the loop n is contained in. NULL if node is in no loop. */
83 ir_loop *get_irn_loop(const ir_node *n);
85 /** Returns outer loop, itself if outermost. */
86 ir_loop *get_loop_outer_loop(const ir_loop *loop);
87 /** Returns nesting depth of this loop */
88 int get_loop_depth(const ir_loop *loop);
90 /* Sons are the inner loops contained in this loop. */
91 /** Returns the number of inner loops */
92 int get_loop_n_sons(const ir_loop *loop);
94 /** Returns the pos`th son loop (inner loop) of a loop.
95 Returns NULL if there is not a pos`th loop_node. */
96 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);
101 /** Returns the pos`th ir_node of a loop.
102 Returns NULL if there is not a pos`th ir_node. */
103 ir_node *get_loop_node(ir_loop *loop, int pos);
105 /** Returns the number of elements contained in loop. */
106 int get_loop_n_elements(const ir_loop *loop);
108 /** Returns a loop element. A loop element can be interpreted as a
109 kind pointer, an ir_node* or an ir_loop*. */
110 loop_element get_loop_element(const ir_loop *loop, int pos);
112 /** Returns the element number of the loop son in loop.
113 * Returns -1 if not found. O(|elements|). */
114 int get_loop_element_pos(const ir_loop *loop, void *le);
116 /** Returns a unique node number for the loop node to make output
117 readable. If libfirm_debug is not set it returns the loop cast to
119 int get_loop_loop_nr(const ir_loop *loop);
121 /** A field to connect additional information to a loop. */
122 void set_loop_link(ir_loop *loop, void *link);
123 void *get_loop_link(const ir_loop *loop);
125 /* ------------------------------------------------------------------- */
126 /* Constructing and destructing the loop/backedge information. */
127 /* ------------------------------------------------------------------- */
129 /** Constructs backedge information and loop tree for a graph in intraprocedural view.
131 * The algorithm views the program representation as a pure graph.
132 * It assumes that only block and phi nodes may be loop headers.
133 * The resulting loop tree is a possible visiting order for dataflow
136 * This algorithm destoyes the link field of block nodes.
138 * @returns Maximal depth of loop tree.
141 * One assumes, the Phi nodes in a block with a backedge have backedges
142 * at the same positions as the block. This is not the case, as
143 * the scc algorithms does not respect the program semantics in this case.
144 * Take a swap in a loop (t = i; i = j; j = t;) This results in two Phi
145 * nodes. They form a cycle. Once the scc algorithm deleted one of the
146 * edges, the cycle is removed. The second Phi node does not get a
149 /* @@@ Well, maybe construct_loop_information or analyze_loops ? */
150 int construct_backedges(ir_graph *irg);
152 #ifdef INTERPROCEDURAL_VIEW
153 /** Constructs backedges for all irgs in interprocedural view.
155 * @see As construct_backedges(), but for interprocedural view.
158 * All loops in the graph will be marked as such, not only
159 * realizeable loops and recursions in the program. E.g., if the
160 * same funcion is called twice, there is a loop between the first
161 * function return and the second call.
163 * @returns Maximal depth of loop tree.
165 int construct_ip_backedges(void);
168 /** Construct loop tree only for control flow.
170 * This constructs loop information resembling the program structure.
171 * It is useful for loop optimizations and analyses, as, e.g., finding
172 * iteration variables or loop invariant code motion.
174 * This algorithm computes only back edge information for Block nodes, not
177 * This algorithm destroyes the link field of block nodes.
179 * @returns Maximal depth of loop tree.
181 int construct_cf_backedges(ir_graph *irg);
183 #ifdef INTERPROCEDURAL_VIEW
184 /** Construct interprocedural loop tree for control flow.
186 * @see construct_cf_backedges() and construct_ip_backedges().
188 int construct_ip_cf_backedges (void);
191 /** Removes all loop information.
192 * Resets all backedges. Works for any construction algorithm.
194 void free_loop_information(ir_graph *irg);
195 void free_all_loop_information (void);
198 /* ------------------------------------------------------------------- */
199 /* Simple analyses based on the loop information */
200 /* ------------------------------------------------------------------- */
202 /** Test whether a value is loop invariant.
204 * @param n The node to be tested.
205 * @param block A block node.
207 * Returns non-zero, if the node n is not changed in the loop block
208 * belongs to or in inner loops of this block. */
209 int is_loop_invariant(ir_node *n, ir_node *block);