renamed entity -> ir_entity
[libfirm] / ir / ana / irloop.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ana/irloop_t.h
4  * Purpose:     Loop datastructure and access functions.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:     7.2002
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2002-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14  * @file irloop.h
15  *
16  *  Computes backedges in the control and data flow.
17  *
18  *  @author Goetz Lindenmaier
19  *
20  *  Only Block and Phi/Filter nodes can have incoming backedges.
21  *  Constructs loops data structure: indicates loop nesting.
22  */
23
24 # ifndef _IRLOOP_H_
25 # define _IRLOOP_H_
26
27 # include "irgraph.h"
28 # include "irnode.h"
29
30 /* ------------------------------------------------------------------- */
31 /*
32  * Backedge information.
33  *
34  * Predecessors of Block, Phi and interprocedural Filter nodes can
35  * have  backedges.  If loop information is computed, this
36  * information is computed, too.
37  * The backedge information can only be used if the graph is not in
38  * phase phase_building.
39  */
40 /* ------------------------------------------------------------------- */
41
42 /** Returns true if the predecessor pos is a backedge in the interprozeduralem view. */
43 int  is_inter_backedge(ir_node *n, int pos);
44 /** Returns true if the predecessor pos is a backedge in the intraprocedural view. */
45 int  is_intra_backedge(ir_node *n, int pos);
46 /** Returns non-zero if the predecessor pos is a backedge. */
47 int is_backedge (ir_node *n, int pos);
48 /** Marks edge pos as a backedge. */
49 void set_backedge (ir_node *n, int pos);
50 /** Marks edge pos as a non-backedge. */
51 void set_not_backedge (ir_node *n, int pos);
52 /** Returns non-zero if n has backedges. */
53 int has_backedges (ir_node *n);
54 /** Clears all backedge information. */
55 void clear_backedges (ir_node *n);
56
57 /* ------------------------------------------------------------------- */
58 /**
59  * The loops data structure.
60  *
61  * The loops data structure represents circles in the intermediate
62  * representation.  It does not represent loops in the terms of a
63  * source program.
64  * Each ir_graph can contain one outermost loop data structure.
65  * loop is the entry point to the nested loops.
66  * The loop data structure contains a field indicating the depth of
67  * the loop within the nesting.  Further it contains a list of the
68  * loops with nesting depth -1.  Finally it contains a list of all
69  * nodes in the loop.
70  *
71  * @todo We could add a field pointing from a node to the containing loop,
72  * this would cost a lot of memory, though.
73  */
74 /* ------------------------------------------------------------------- */
75 #ifndef _IR_LOOP_TYPEDEF_
76 #define _IR_LOOP_TYPEDEF_
77 typedef struct ir_loop ir_loop;
78 #endif
79
80 /** Loop elements: loop nodes and ir nodes */
81 typedef union {
82     firm_kind *kind;    /**< is either k_ir_node or k_ir_loop */
83     ir_node *node;      /**< Pointer to an ir_node element */
84     ir_loop *son;       /**< Pointer to an ir_loop element */
85 } loop_element;
86
87 int      is_ir_loop(const void *thing);
88
89 /** Set the outermost loop in ir graph as basic access to loop tree. */
90 void     set_irg_loop(ir_graph *irg, ir_loop *l);
91
92 /* Returns the root loop info (if exists) for an irg. */
93 ir_loop *get_irg_loop(ir_graph *irg);
94
95 /** Returns the loop n is contained in.  NULL if node is in no loop. */
96 ir_loop *get_irn_loop(const ir_node *n);
97
98 /** Returns outer loop, itself if outermost. */
99 ir_loop *get_loop_outer_loop (const ir_loop *loop);
100 /** Returns nesting depth of this loop */
101 int      get_loop_depth (const ir_loop *loop);
102
103 /* Sons are the inner loops contained in this loop. */
104 /** Returns the number of inner loops */
105 int      get_loop_n_sons (const ir_loop *loop);
106
107 /** Returns the pos`th son loop (inner loop) of a loop.
108     Returns NULL if there is not a pos`th loop_node. */
109 ir_loop *get_loop_son (ir_loop *loop, int pos);
110
111 /** Returns the number of nodes contained in loop.  */
112 int      get_loop_n_nodes (ir_loop *loop);
113
114 /** Returns the pos`th ir_node of a loop.
115     Returns NULL if there is not a pos`th ir_node. */
116 ir_node *get_loop_node (ir_loop *loop, int pos);
117
118 /** Returns the number of elements contained in loop.  */
119 int      get_loop_n_elements (const ir_loop *loop);
120
121 /** Returns a loop element.  A loop element can be interpreted as a
122     kind pointer, an ir_node* or an ir_loop*. */
123 loop_element get_loop_element (const ir_loop *loop, int pos);
124
125 /** Returns the element number of the loop son in loop.
126  *  Returns -1 if not found. O(|elements|). */
127 int get_loop_element_pos(const ir_loop *loop, void *le);
128
129 /** Returns a unique node number for the loop node to make output
130     readable. If libfirm_debug is not set it returns the loop cast to
131     int. */
132 int get_loop_loop_nr(const ir_loop *loop);
133
134 /** A field to connect additional information to a loop.  Only valid
135     if libfirm_debug is set, else returns NULL.  */
136 void  set_loop_link (ir_loop *loop, void *link);
137 void *get_loop_link (const ir_loop *loop);
138
139 /* ------------------------------------------------------------------- */
140 /* Constructing and destructing the loop/backedge information.         */
141 /* ------------------------------------------------------------------- */
142
143 /** Constructs backedge information and loop tree for a graph in intraprocedural view.
144  *
145  *  The algorithm views the program representation as a pure graph.
146  *  It assumes that only block and phi nodes may be loop headers.
147  *  The resulting loop tree is a possible visiting order for dataflow
148  *  analysis.
149  *
150  *  This algorithm destoyes the link field of block nodes.
151  *
152  *  @returns Maximal depth of loop tree.
153  *
154  *  @remark
155  *  One assumes, the Phi nodes in a block with a backedge have backedges
156  *  at the same positions as the block.  This is not the case, as
157  *  the scc algorithms does not respect the program semantics in this case.
158  *  Take a swap in a loop (t = i; i = j; j = t;)  This results in two Phi
159  *  nodes.  They form a cycle.  Once the scc algorithm deleted one of the
160  *  edges, the cycle is removed.  The second Phi node does not get a
161  *  backedge!
162  */
163 /* @@@ Well, maybe construct_loop_information or analyze_loops ? */
164 int construct_backedges(ir_graph *irg);
165
166 /** Constructs backedges for all irgs in interprocedural view.
167  *
168  *  @see As construct_backedges(), but for interprocedural view.
169  *
170  *  @remark
171  *  All loops in the graph will be marked as such, not only
172  *  realizeable loops and recursions in the program.  E.g., if the
173  *  same funcion is called twice, there is a loop between the first
174  *  function return and the second call.
175  *
176  *  @returns Maximal depth of loop tree.
177 */
178 int construct_ip_backedges(void);
179
180 /** Construct loop tree only for control flow.
181  *
182  *  This constructs loop information resembling the program structure.
183  *  It is useful for loop optimizations and analyses, as, e.g., finding
184  *  iteration variables or loop invariant code motion.
185  *
186  *  This algorithm computes only back edge information for Block nodes, not
187  *  for Phi nodes.
188  *
189  *  This algorithm destoyes the link field of block nodes.
190  *
191  * @returns Maximal depth of loop tree.
192  */
193 int construct_cf_backedges(ir_graph *irg);
194
195 /** Construct interprocedural loop tree for control flow.
196  *
197  *  @see construct_cf_backedges() and construct_ip_backedges().
198  */
199 int construct_ip_cf_backedges (void);
200
201 /** Removes all loop information.
202  *  Resets all backedges.  Works for any construction algorithm.
203  */
204 void free_loop_information(ir_graph *irg);
205 void free_all_loop_information (void);
206
207
208
209
210 /* ------------------------------------------------------------------- */
211 /* Simple analyses based on the loop information                       */
212 /* ------------------------------------------------------------------- */
213
214 /** Test whether a value is loop invariant.
215  *
216  * @param n      The node to be tested.
217  * @param block  A block node.
218  *
219  * Returns non-zero, if the node n is not changed in the loop block
220  * belongs to or in inner loops of this block. */
221 int is_loop_invariant(ir_node *n, ir_node *block);
222
223
224 #endif /* _IRLOOP_H_ */