beifg: Remove the unused function be_ifg_nodes_break().
[libfirm] / include / libfirm / irloop.h
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief    Loop datastructure and access functions.
9  * @author   Goetz Lindenmaier
10  * @date     7.2002
11  * @brief
12  *  Computes backedges in the control and data flow.
13  *
14  * @note
15  *  Only Block and Phi nodes can have incoming backedges.
16  *  Constructs loops data structure: indicates loop nesting.
17  */
18 #ifndef FIRM_ANA_IRLOOP_H
19 #define FIRM_ANA_IRLOOP_H
20
21 #include "firm_types.h"
22 #include "firm_common.h"
23 #include "begin.h"
24
25 /** @ingroup irana
26  * @defgroup ir_loop Loops
27  * @{
28  */
29
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);
40
41 /** Loop elements: loop nodes and ir nodes */
42 typedef union {
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) */
47 } loop_element;
48
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
51  * header */
52 FIRM_API int is_ir_loop(const void *thing);
53
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);
56
57 /** Returns the root loop info (if exists) for an irg. */
58 FIRM_API ir_loop *get_irg_loop(const ir_graph *irg);
59
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);
62
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);
67
68 /** Returns the number of elements contained in loop.  */
69 FIRM_API size_t get_loop_n_elements(const ir_loop *loop);
70
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);
74
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
77 int. */
78 FIRM_API long get_loop_loop_nr(const ir_loop *loop);
79
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);
85
86 /** Constructs backedge information and loop tree for a graph.
87  *
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
91  *  analysis.
92  *
93  *  This algorithm destoyes the link field of block nodes.
94  *
95  *  @remark
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
102  *  backedge!
103  */
104 FIRM_API void construct_backedges(ir_graph *irg);
105
106 /**
107  * Construct Intra-procedural control flow loop tree for a IR-graph.
108  *
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.
112  *
113  * This algorithm computes only back edge information for Block nodes, not
114  * for Phi nodes.
115  *
116  * This algorithm destroyes the link field of block nodes.
117  *
118  * @param irg  the graph
119  */
120 FIRM_API void construct_cf_backedges(ir_graph *irg);
121
122 /**
123  * Computes Intra-procedural control flow loop tree on demand.
124  *
125  * @param irg  the graph
126  */
127 FIRM_API void assure_loopinfo(ir_graph *irg);
128
129 /**
130  * Removes all loop information.
131  * Resets all backedges.  Works for any construction algorithm.
132  */
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);
136
137 /** Tests whether a value is loop invariant.
138  *
139  * @param n      The node to be tested.
140  * @param block  A block node.
141  *
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);
145
146 /** @} */
147
148 #include "end.h"
149
150 #endif