2 * Copyright (C) 1995-2011 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 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"
37 #include "firm_common.h"
40 /** Returns non-zero if the predecessor pos is a backedge. */
41 FIRM_API int is_backedge(const ir_node *n, int pos);
42 /** Marks edge pos as a backedge. */
43 FIRM_API void set_backedge(ir_node *n, int pos);
44 /** Marks edge pos as a non-backedge. */
45 FIRM_API void set_not_backedge(ir_node *n, int pos);
46 /** Returns non-zero if n has backedges. */
47 FIRM_API int has_backedges(const ir_node *n);
48 /** Clears all backedge information. */
49 FIRM_API void clear_backedges(ir_node *n);
51 /** Loop elements: loop nodes and ir nodes */
53 firm_kind *kind; /**< is either k_ir_node or k_ir_loop */
54 ir_node *node; /**< Pointer to an ir_node element */
55 ir_loop *son; /**< Pointer to an ir_loop element */
56 ir_graph *irg; /**< Pointer to an ir_graph element (only callgraph loop trees) */
59 FIRM_API int is_ir_loop(const void *thing);
61 /** Set the outermost loop in ir graph as basic access to loop tree. */
62 FIRM_API void set_irg_loop(ir_graph *irg, ir_loop *l);
64 /** Returns the root loop info (if exists) for an irg. */
65 FIRM_API ir_loop *get_irg_loop(const ir_graph *irg);
67 /** Returns the loop n is contained in. NULL if node is in no loop. */
68 FIRM_API ir_loop *get_irn_loop(const ir_node *n);
70 /** Returns outer loop, itself if outermost. */
71 FIRM_API ir_loop *get_loop_outer_loop(const ir_loop *loop);
72 /** Returns nesting depth of this loop */
73 FIRM_API unsigned get_loop_depth(const ir_loop *loop);
75 /** Returns the number of inner loops */
76 FIRM_API size_t get_loop_n_sons(const ir_loop *loop);
78 /** Returns the pos`th son loop (inner loop) of a loop.
79 Returns NULL if there is not a pos`th loop_node. */
80 FIRM_API ir_loop *get_loop_son(ir_loop *loop, size_t pos);
82 /** Returns the number of nodes contained in loop. */
83 FIRM_API size_t get_loop_n_nodes(const ir_loop *loop);
85 /** Returns the pos`th ir_node of a loop.
86 Returns NULL if there is not a pos`th ir_node. */
87 FIRM_API ir_node *get_loop_node(const ir_loop *loop, size_t pos);
89 /** Returns the number of elements contained in loop. */
90 FIRM_API size_t get_loop_n_elements(const ir_loop *loop);
92 /** Returns a loop element. A loop element can be interpreted as a
93 kind pointer, an ir_node* or an ir_loop*. */
94 FIRM_API loop_element get_loop_element(const ir_loop *loop, size_t pos);
96 #define INVALID_LOOP_POS ((size_t)-1)
98 /** Returns the element number of the loop son in loop.
99 * Returns INVALID_LOOP_POS if not found. O(|elements|). */
100 FIRM_API size_t get_loop_element_pos(const ir_loop *loop, void *le);
102 /** Returns a unique node number for the loop node to make output
103 readable. If libfirm_debug is not set it returns the loop cast to
105 FIRM_API long get_loop_loop_nr(const ir_loop *loop);
107 /** A field to connect additional information to a loop. */
108 FIRM_API void set_loop_link(ir_loop *loop, void *link);
109 FIRM_API void *get_loop_link(const ir_loop *loop);
111 /** Constructs backedge information and loop tree for a graph.
113 * The algorithm views the program representation as a pure graph.
114 * It assumes that only block and phi nodes may be loop headers.
115 * The resulting loop tree is a possible visiting order for dataflow
118 * This algorithm destoyes the link field of block nodes.
120 * @returns Maximal depth of loop tree.
123 * One assumes, the Phi nodes in a block with a backedge have backedges
124 * at the same positions as the block. This is not the case, as
125 * the scc algorithms does not respect the program semantics in this case.
126 * Take a swap in a loop (t = i; i = j; j = t;) This results in two Phi
127 * nodes. They form a cycle. Once the scc algorithm deleted one of the
128 * edges, the cycle is removed. The second Phi node does not get a
131 FIRM_API int construct_backedges(ir_graph *irg);
134 * Construct Intra-procedural control flow loop tree for a IR-graph.
136 * This constructs loop information resembling the program structure.
137 * It is useful for loop optimizations and analyses, as, e.g., finding
138 * iteration variables or loop invariant code motion.
140 * This algorithm computes only back edge information for Block nodes, not
143 * This algorithm destroyes the link field of block nodes.
145 * @param irg the graph
147 * @returns Maximal depth of loop tree.
149 FIRM_API int construct_cf_backedges(ir_graph *irg);
152 * Computes Intra-procedural control flow loop tree on demand.
154 * @param irg the graph
156 FIRM_API void assure_loopinfo(ir_graph *irg);
159 * Removes all loop information.
160 * Resets all backedges. Works for any construction algorithm.
162 FIRM_API void free_loop_information(ir_graph *irg);
163 FIRM_API void free_all_loop_information (void);
165 /** Test whether a value is loop invariant.
167 * @param n The node to be tested.
168 * @param block A block node.
170 * Returns non-zero, if the node n is not changed in the loop block
171 * belongs to or in inner loops of this block. */
172 FIRM_API int is_loop_invariant(const ir_node *n, const ir_node *block);