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
39 /* ------------------------------------------------------------------- */
41 * Backedge information.
43 * Predecessors of Block, Phi and interprocedural Filter nodes can
44 * have backedges. If loop information is computed, this
45 * information is computed, too.
46 * The backedge information can only be used if the graph is not in
47 * phase phase_building.
49 /* ------------------------------------------------------------------- */
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 /** Returns non-zero if the predecessor pos is a backedge. */
56 int is_backedge (ir_node *n, int pos);
57 /** Marks edge pos as a backedge. */
58 void set_backedge (ir_node *n, int pos);
59 /** Marks edge pos as a non-backedge. */
60 void set_not_backedge (ir_node *n, int pos);
61 /** Returns non-zero if n has backedges. */
62 int has_backedges (ir_node *n);
63 /** Clears all backedge information. */
64 void clear_backedges (ir_node *n);
66 /* ------------------------------------------------------------------- */
68 * The loops data structure.
70 * The loops data structure represents circles in the intermediate
71 * representation. It does not represent loops in the terms of a
73 * Each ir_graph can contain one outermost loop data structure.
74 * loop is the entry point to the nested loops.
75 * The loop data structure contains a field indicating the depth of
76 * the loop within the nesting. Further it contains a list of the
77 * loops with nesting depth -1. Finally it contains a list of all
80 * @todo We could add a field pointing from a node to the containing loop,
81 * this would cost a lot of memory, though.
83 /* ------------------------------------------------------------------- */
84 #ifndef _IR_LOOP_TYPEDEF_
85 #define _IR_LOOP_TYPEDEF_
86 typedef struct ir_loop ir_loop;
89 /** Loop elements: loop nodes and ir nodes */
91 firm_kind *kind; /**< is either k_ir_node or k_ir_loop */
92 ir_node *node; /**< Pointer to an ir_node element */
93 ir_loop *son; /**< Pointer to an ir_loop element */
96 int is_ir_loop(const void *thing);
98 /** Set the outermost loop in ir graph as basic access to loop tree. */
99 void set_irg_loop(ir_graph *irg, ir_loop *l);
101 /* Returns the root loop info (if exists) for an irg. */
102 ir_loop *get_irg_loop(ir_graph *irg);
104 /** Returns the loop n is contained in. NULL if node is in no loop. */
105 ir_loop *get_irn_loop(const ir_node *n);
107 /** Returns outer loop, itself if outermost. */
108 ir_loop *get_loop_outer_loop (const ir_loop *loop);
109 /** Returns nesting depth of this loop */
110 int get_loop_depth (const ir_loop *loop);
112 /* Sons are the inner loops contained in this loop. */
113 /** Returns the number of inner loops */
114 int get_loop_n_sons (const ir_loop *loop);
116 /** Returns the pos`th son loop (inner loop) of a loop.
117 Returns NULL if there is not a pos`th loop_node. */
118 ir_loop *get_loop_son (ir_loop *loop, int pos);
120 /** Returns the number of nodes contained in loop. */
121 int get_loop_n_nodes (ir_loop *loop);
123 /** Returns the pos`th ir_node of a loop.
124 Returns NULL if there is not a pos`th ir_node. */
125 ir_node *get_loop_node (ir_loop *loop, int pos);
127 /** Returns the number of elements contained in loop. */
128 int get_loop_n_elements (const ir_loop *loop);
130 /** Returns a loop element. A loop element can be interpreted as a
131 kind pointer, an ir_node* or an ir_loop*. */
132 loop_element get_loop_element (const ir_loop *loop, int pos);
134 /** Returns the element number of the loop son in loop.
135 * Returns -1 if not found. O(|elements|). */
136 int get_loop_element_pos(const ir_loop *loop, void *le);
138 /** Returns a unique node number for the loop node to make output
139 readable. If libfirm_debug is not set it returns the loop cast to
141 int get_loop_loop_nr(const ir_loop *loop);
143 /** A field to connect additional information to a loop. Only valid
144 if libfirm_debug is set, else returns NULL. */
145 void set_loop_link (ir_loop *loop, void *link);
146 void *get_loop_link (const ir_loop *loop);
148 /* ------------------------------------------------------------------- */
149 /* Constructing and destructing the loop/backedge information. */
150 /* ------------------------------------------------------------------- */
152 /** Constructs backedge information and loop tree for a graph in intraprocedural view.
154 * The algorithm views the program representation as a pure graph.
155 * It assumes that only block and phi nodes may be loop headers.
156 * The resulting loop tree is a possible visiting order for dataflow
159 * This algorithm destoyes the link field of block nodes.
161 * @returns Maximal depth of loop tree.
164 * One assumes, the Phi nodes in a block with a backedge have backedges
165 * at the same positions as the block. This is not the case, as
166 * the scc algorithms does not respect the program semantics in this case.
167 * Take a swap in a loop (t = i; i = j; j = t;) This results in two Phi
168 * nodes. They form a cycle. Once the scc algorithm deleted one of the
169 * edges, the cycle is removed. The second Phi node does not get a
172 /* @@@ Well, maybe construct_loop_information or analyze_loops ? */
173 int construct_backedges(ir_graph *irg);
175 /** Constructs backedges for all irgs in interprocedural view.
177 * @see As construct_backedges(), but for interprocedural view.
180 * All loops in the graph will be marked as such, not only
181 * realizeable loops and recursions in the program. E.g., if the
182 * same funcion is called twice, there is a loop between the first
183 * function return and the second call.
185 * @returns Maximal depth of loop tree.
187 int construct_ip_backedges(void);
189 /** Construct loop tree only for control flow.
191 * This constructs loop information resembling the program structure.
192 * It is useful for loop optimizations and analyses, as, e.g., finding
193 * iteration variables or loop invariant code motion.
195 * This algorithm computes only back edge information for Block nodes, not
198 * This algorithm destoyes the link field of block nodes.
200 * @returns Maximal depth of loop tree.
202 int construct_cf_backedges(ir_graph *irg);
204 /** Construct interprocedural loop tree for control flow.
206 * @see construct_cf_backedges() and construct_ip_backedges().
208 int construct_ip_cf_backedges (void);
210 /** Removes all loop information.
211 * Resets all backedges. Works for any construction algorithm.
213 void free_loop_information(ir_graph *irg);
214 void free_all_loop_information (void);
219 /* ------------------------------------------------------------------- */
220 /* Simple analyses based on the loop information */
221 /* ------------------------------------------------------------------- */
223 /** Test whether a value is loop invariant.
225 * @param n The node to be tested.
226 * @param block A block node.
228 * Returns non-zero, if the node n is not changed in the loop block
229 * belongs to or in inner loops of this block. */
230 int is_loop_invariant(ir_node *n, ir_node *block);