added average calculation of distribution table
[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 predesessor pos is a backedge. */
43 int  is_inter_backedge(ir_node *n, int pos);
44 int  is_intra_backedge(ir_node *n, int pos);
45 /** Returns non-zero if the predecessor pos is a backedge. */
46 int is_backedge (ir_node *n, int pos);
47 /** Remarks that edge pos is a backedge. */
48 void set_backedge (ir_node *n, int pos);
49 /** Remarks that edge pos is not a backedge. */
50 void set_not_backedge (ir_node *n, int pos);
51 /** Returns non-zero if n has backedges. */
52 int has_backedges (ir_node *n);
53 /** Sets backedge information to zero. */
54 void clear_backedges (ir_node *n);
55
56 /* ------------------------------------------------------------------- */
57 /**
58  * The loops datastructure.
59  *
60  * The loops datastructure represents circles in the intermediate
61  * representation.  It does not represent loops in the terms of a
62  * source program.
63  * Each ir_graph can contain one outermost loop datastructure.
64  * loop is the entry point to the nested loops.
65  * The loop datastructure contains a field indicating the depth of
66  * the loop within the nesting.  Further it contains a list of the
67  * loops with nesting depth -1.  Finally it contains a list of all
68  * nodes in the loop.
69  *
70  * @todo We could add a field pointing from a node to the containing loop,
71  * this would cost a lot of memory, though.
72  */
73 /* ------------------------------------------------------------------- */
74 #ifndef _IR_LOOP_TYPEDEF_
75 #define _IR_LOOP_TYPEDEF_
76 typedef struct ir_loop ir_loop;
77 #endif
78
79 /** Loop elements: loop nodes and ir nodes */
80 typedef union {
81     firm_kind *kind;    /**< is either k_ir_node or k_ir_loop */
82     ir_node *node;      /**< Pointer to an ir_node element */
83     ir_loop *son;       /**< Pointer to an ir_loop element */
84 } loop_element;
85
86 int      is_ir_loop(const void *thing);
87
88 /** Set the outermost loop in ir graph as basic access to loop tree. */
89 void     set_irg_loop(ir_graph *irg, ir_loop *l);
90
91 /* Returns the root loop info (if exists) for an irg. */
92 ir_loop *get_irg_loop(ir_graph *irg);
93
94 /** Returns the loop n is contained in.  NULL if node is in no loop. */
95 ir_loop *get_irn_loop(ir_node *n);
96
97 /** Returns outer loop, itself if outermost. */
98 ir_loop *get_loop_outer_loop (ir_loop *loop);
99 /** Returns nesting depth of this loop */
100 int      get_loop_depth (ir_loop *loop);
101
102 /* Sons are the inner loops contained in this loop. */
103 /** Returns the number of inner loops */
104 int      get_loop_n_sons (ir_loop *loop);
105
106 /** Returns the pos`th son loop (inner loop) of a loop.
107     Returns NULL if there is not a pos`th loop_node. */
108 ir_loop *get_loop_son (ir_loop *loop, int pos);
109
110 /** Returns the number of nodes contained in loop.  */
111 int      get_loop_n_nodes (ir_loop *loop);
112
113 /** Returns the pos`th ir_node of a loop.
114     Returns NULL if there is not a pos`th ir_node. */
115 ir_node *get_loop_node (ir_loop *loop, int pos);
116
117 /** Returns the number of elements contained in loop.  */
118 int      get_loop_n_elements (ir_loop *loop);
119 /** Returns a loop element.  A loop element can be interpreted as a
120     kind pointer, an ir_node* or an ir_loop*. */
121 loop_element get_loop_element (ir_loop *loop, int pos);
122
123 /** Returns the element number of the loop son in loop.
124  *  Returns -1 if not found. O(|elements|). */
125 int get_loop_element_pos(ir_loop *loop, void *le);
126
127 /** Returns a unique node number for the loop node to make output
128     readable. If libfirm_debug is not set it returns the loop cast to
129     int. */
130 int get_loop_loop_nr(ir_loop *loop);
131
132 /** A field to connect additional information to a loop.  Only valid
133     if libfirm_debug is set, else returns NULL.  */
134 void  set_loop_link (ir_loop *loop, void *link);
135 void *get_loop_link (const ir_loop *loop);
136
137 /* ------------------------------------------------------------------- */
138 /* Constructing and destructing the loop/backedge information.         */
139 /* ------------------------------------------------------------------- */
140
141 /** Constructs backedge information and loop tree for a graph in intraprocedural view.
142  *
143  *  The algorithm views the program representation as a pure graph.
144  *  It assumes that only block and phi nodes may be loop headers.
145  *  The resulting loop tree is a possible visiting order for dataflow
146  *  analysis.
147  *
148  *  @returns Maximal depth of loop tree.
149  *
150  *  @remark
151  *  One assumes, the Phi nodes in a block with a backedge have backedges
152  *  at the same positions as the block.  This is not the case, as
153  *  the scc algorithms does not respect the program semantics in this case.
154  *  Take a swap in a loop (t = i; i = j; j = t;)  This results in two Phi
155  *  nodes.  They form a cycle.  Once the scc algorithm deleted one of the
156  *  edges, the cycle is removed.  The second Phi node does not get a
157  *  backedge!
158  */
159 /* @@@ Well, maybe construct_loop_information or analyze_loops ? */
160 int construct_backedges(ir_graph *irg);
161
162 /** Constructs backedges for all irgs in interprocedural view.
163  *
164  *  @see As construct_backedges(), but for interprocedural view.
165  *
166  *  @remark
167  *  All loops in the graph will be marked as such, not only
168  *  realizeable loops and recursions in the program.  E.g., if the
169  *  same funcion is called twice, there is a loop between the first
170  *  function return and the second call.
171  *
172  *  @returns Maximal depth of loop tree.
173 */
174 int construct_ip_backedges(void);
175
176 /** Construct loop tree only for control flow.
177  *
178  *  This constructs loop information resembling the program structure.
179  *  It is useful for loop optimizations and analyses, as, e.g., finding
180  *  iteration variables or loop invariant code motion.
181  *
182  *  This algorithm computes only back edge information for Block nodes, not
183  *  for Phi nodes.
184  *
185  * @returns Maximal depth of loop tree. */
186 int construct_cf_backedges(ir_graph *irg);
187
188 /** Construct interprocedural loop tree for control flow.
189  *
190  *  @see construct_cf_backedges() and construct_ip_backedges().
191  */
192 int construct_ip_cf_backedges (void);
193
194 /** Removes all loop information.
195  *  Resets all backedges.  Works for any construction algorithm.
196  */
197 void free_loop_information(ir_graph *irg);
198 void free_all_loop_information (void);
199
200
201
202
203 /* ------------------------------------------------------------------- */
204 /* Simple analyses based on the loop information                       */
205 /* ------------------------------------------------------------------- */
206
207 /** Test whether a value is loop invariant.
208  *
209  * @param n      The node to be tested.
210  * @param block  A block node.
211  *
212  * Returns non-zero, if the node n is not changed in the loop block
213  * belongs to or in inner loops of this block. */
214 int is_loop_invariant(ir_node *n, ir_node *block);
215
216
217 #endif /* _IRLOOP_H_ */