2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Loop datastructure and access functions.
9 * @author Goetz Lindenmaier
12 * Computes backedges in the control and data flow.
15 * Only Block and Phi nodes can have incoming backedges.
16 * Constructs loops data structure: indicates loop nesting.
18 #ifndef FIRM_ANA_IRLOOP_H
19 #define FIRM_ANA_IRLOOP_H
21 #include "firm_types.h"
22 #include "firm_common.h"
26 * @defgroup ir_loop Loops
30 /** Returns non-zero if the predecessor pos is a backedge. */
31 FIRM_API int is_backedge(const ir_node *n, int pos);
32 /** Marks edge pos as a backedge. */
33 FIRM_API void set_backedge(ir_node *n, int pos);
34 /** Marks edge pos as a non-backedge. */
35 FIRM_API void set_not_backedge(ir_node *n, int pos);
36 /** Returns non-zero if n has backedges. */
37 FIRM_API int has_backedges(const ir_node *n);
38 /** Clears all backedge information. */
39 FIRM_API void clear_backedges(ir_node *n);
41 /** Loop elements: loop nodes and ir nodes */
43 firm_kind *kind; /**< is either k_ir_node or k_ir_loop */
44 ir_node *node; /**< Pointer to an ir_node element */
45 ir_loop *son; /**< Pointer to an ir_loop element */
46 ir_graph *irg; /**< Pointer to an ir_graph element (only callgraph loop trees) */
49 /** Tests whether a given pointer points to a loop.
50 * @note only works reliably if @p thing points to something with a ::firm_kind
52 FIRM_API int is_ir_loop(const void *thing);
54 /** Sets the outermost loop in ir graph as basic access to loop tree. */
55 FIRM_API void set_irg_loop(ir_graph *irg, ir_loop *l);
57 /** Returns the root loop info (if exists) for an irg. */
58 FIRM_API ir_loop *get_irg_loop(const ir_graph *irg);
60 /** Returns the loop n is contained in. NULL if node is in no loop. */
61 FIRM_API ir_loop *get_irn_loop(const ir_node *n);
63 /** Returns outer loop, itself if outermost. */
64 FIRM_API ir_loop *get_loop_outer_loop(const ir_loop *loop);
65 /** Returns nesting depth of this loop */
66 FIRM_API unsigned get_loop_depth(const ir_loop *loop);
68 /** Returns the number of elements contained in loop. */
69 FIRM_API size_t get_loop_n_elements(const ir_loop *loop);
71 /** Returns a loop element. A loop element can be interpreted as a
72 * kind pointer, an ir_node* or an ir_loop*. */
73 FIRM_API loop_element get_loop_element(const ir_loop *loop, size_t pos);
75 /** Returns a unique node number for the loop node to make output
76 readable. If libfirm_debug is not set it returns the loop cast to
78 FIRM_API long get_loop_loop_nr(const ir_loop *loop);
80 /** A field to connect additional information to a loop. */
81 FIRM_API void set_loop_link(ir_loop *loop, void *link);
82 /** Returns field with additional loop information.
83 * @see set_loop_link() */
84 FIRM_API void *get_loop_link(const ir_loop *loop);
86 /** Constructs backedge information and loop tree for a graph.
88 * The algorithm views the program representation as a pure graph.
89 * It assumes that only block and phi nodes may be loop headers.
90 * The resulting loop tree is a possible visiting order for dataflow
93 * This algorithm destoyes the link field of block nodes.
96 * One assumes, the Phi nodes in a block with a backedge have backedges
97 * at the same positions as the block. This is not the case, as
98 * the scc algorithms does not respect the program semantics in this case.
99 * Take a swap in a loop (t = i; i = j; j = t;) This results in two Phi
100 * nodes. They form a cycle. Once the scc algorithm deleted one of the
101 * edges, the cycle is removed. The second Phi node does not get a
104 FIRM_API void construct_backedges(ir_graph *irg);
107 * Construct Intra-procedural control flow loop tree for a IR-graph.
109 * This constructs loop information resembling the program structure.
110 * It is useful for loop optimizations and analyses, as, e.g., finding
111 * iteration variables or loop invariant code motion.
113 * This algorithm computes only back edge information for Block nodes, not
116 * This algorithm destroyes the link field of block nodes.
118 * @param irg the graph
120 FIRM_API void construct_cf_backedges(ir_graph *irg);
123 * Computes Intra-procedural control flow loop tree on demand.
125 * @param irg the graph
127 FIRM_API void assure_loopinfo(ir_graph *irg);
130 * Removes all loop information.
131 * Resets all backedges. Works for any construction algorithm.
133 FIRM_API void free_loop_information(ir_graph *irg);
134 /** Removes loop information from all graphs in the current program. */
135 FIRM_API void free_all_loop_information(void);
137 /** Tests whether a value is loop invariant.
139 * @param n The node to be tested.
140 * @param block A block node.
142 * Returns non-zero, if the node n is not changed in the loop block
143 * belongs to or in inner loops of this block. */
144 FIRM_API int is_loop_invariant(const ir_node *n, const ir_node *block);