fixed some depencies between irdump.c and irdumptxt.c
[libfirm] / ir / ir / irgraph.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irgraph.c
4  * Purpose:     Entry point to the representation of procedure code.
5  * Author:      Martin Trapp, Christian Schaefer
6  * Modified by: Goetz Lindenmaier
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14  * @file irgraph.h
15  *
16  * ir graph construction.
17  *
18  * @author Martin Trapp, Christian Schaefer
19  */
20
21
22 #include "irop.h"
23
24 # ifndef _IRGRAPH_H_
25 # define _IRGRAPH_H_
26 # include "tv.h"
27 # include "entity.h"
28
29 /* to resolve recursion between irnode.h and irgraph.h */
30 #ifndef _IR_NODE_TYPEDEF_
31 #define _IR_NODE_TYPEDEF_
32 typedef struct ir_node ir_node;
33 #endif
34
35 /* to resolve recursion between entity.h and irgraph.h */
36 #ifndef _IR_GRAPH_TYPEDEF_
37 #define _IR_GRAPH_TYPEDEF_
38 typedef struct ir_graph ir_graph;
39 #endif
40
41 /**
42  *
43  * NAME  Datastructure that holds central information about a procedure
44  *
45  *    ir_graph *new_ir_graph (entity *ent, int params);
46  *    -------------------------------------------------
47  *
48  *    This constructor generates the basic infrastructure needed to
49  *    represent a procedure in FIRM.
50  *
51  *    The parameters of new_ir_graph are:
52  *
53  *      *ent             A pointer to an entity representing the procedure.
54  *
55  *      params           An integer giving the number of local variables in the
56  *                       procedure.
57  *
58  *    It allocates an ir_graph and sets current_ir_graph to point to this
59  *    graph.  Further it allocates the following nodes needed for every
60  *    procedure:
61  *
62  *    * The start block containing a start node and Proj nodes for it's
63  *      five results (X, M, P, P, T).
64  *    * The end block containing an end node. This block is not matured
65  *      after executing new_ir_graph as predecessors need to be added to it.
66  *      (Maturing a block means fixing it's number of predecessors.)
67  *    * The current block, which is empty and also not matured.
68  *
69  *    Further it enters the global store into the datastructure of the start
70  *    block that contanis all valid values in this block (set_store()).  This
71  *    datastructure is used to build the Phi nodes and removed after
72  *    completion of the graph.  There is no path from end to start in the
73  *    graph after calling ir_graph.
74  *    op_pin_state_pinned    set to "op_pin_state_pinned" if no global cse was performed on the graph.
75  *             set to "op_pin_state_floats" if global cse was performed (and during construction:
76  *             did actually change something).  Code placement is necessary.
77  */
78
79 /** Global variable holding the current ir graph.
80  *
81  *  This global variable is used by the ir construction
82  *  interface in ircons and by the optimizations.
83  */
84 extern ir_graph *current_ir_graph;
85
86 ir_graph *get_current_ir_graph(void);
87 void      set_current_ir_graph(ir_graph *graph);
88
89 /** This flag indicate the current view. The behaviour of some methods
90  * (get_irn_*, set_irn_*) is influenced by this flag. */
91 int get_interprocedural_view(void);
92 void set_interprocedural_view(int state);
93
94 /** Create a new ir graph to build ir for a procedure.
95  *
96  *  ent is the entity representing this procedure, i.e., the type of the
97  *  entity must be of a method type.  The constructor automatically sets the
98  *  field irg of the entity as well as current_ir_graph to the new ir graph.
99  *  n_loc is the number of local variables in this procedure including
100  *  the procedure parameters.
101  *  The constructor adds the new irgraph to the list in ir_prog.
102  *  The state of the ir graph is:  phase_building, op_pin_state_pinned, outs_none.
103  *
104  *  @see new_pseudo_ir_graph() */
105 ir_graph *new_ir_graph (entity *ent, int n_loc);
106
107 /** Frees the passed irgraph.
108  * Deallocates all nodes in this graph and the ir_graph structure.
109  * Sets the field irgraph in the corresponding entity to NULL.
110  * Does not remove the irgraph from the list in irprog (requires
111  * inefficient search, call remove_irp_irg by hand).
112  * Does not free types, entities or modes that are used only by this
113  * graph, nor the entity standing for this graph.
114  */
115 void free_ir_graph (ir_graph *irg);
116
117 /* --- access routines for all ir_graph attributes --- */
118
119 /**
120  *   Checks whether a pointer points to a ir graph.
121  *
122  *   @param thing     an arbitrary pointer
123  *
124  *   @return
125  *       true if the thing is a ir graph, else false
126  */
127 int      is_ir_graph(const void *thing);
128
129 /* #define get_irg_entity get_irg_ent */
130 /* #define set_irg_entity set_irg_ent */
131 entity  *get_irg_entity (const ir_graph *irg);
132 void     set_irg_entity (ir_graph *irg, entity *ent);
133
134 type    *get_irg_frame_type (const ir_graph *irg);
135 void     set_irg_frame_type (ir_graph *irg, type *ftp);
136 /* To test for a frame type. O(#irgs) if ftp is class type.  */
137 int      is_frame_type (const type *ftp);
138
139 ir_node *get_irg_start_block (const ir_graph *irg);
140 void     set_irg_start_block (ir_graph *irg, ir_node *node);
141
142 ir_node *get_irg_start (const ir_graph *irg);
143 void     set_irg_start (ir_graph *irg, ir_node *node);
144
145 ir_node *get_irg_end_block (const ir_graph *irg);
146 void     set_irg_end_block (ir_graph *irg, ir_node *node);
147
148 ir_node *get_irg_end (const ir_graph *irg);
149 void     set_irg_end (ir_graph *irg, ir_node *node);
150
151 /* The fields end_reg and end_except contain the end nodes of the
152    interprocedural view.  If the view is not constructed they contain
153    the nomal end node. */
154 ir_node *get_irg_end_reg (const ir_graph *irg);
155 void     set_irg_end_reg (ir_graph *irg, ir_node *node);
156
157 ir_node *get_irg_end_except (const ir_graph *irg);
158 void     set_irg_end_except (ir_graph *irg, ir_node *node);
159
160
161 /* @@@ oblivious, no more supported. */
162 ir_node *get_irg_cstore (const ir_graph *irg);
163 void     set_irg_cstore (ir_graph *irg, ir_node *node);
164 /* end oblivious */
165
166 /** Returns the node that represents the frame pointer. */
167 ir_node *get_irg_frame (const ir_graph *irg);
168 /** Sets the node that represents the frame pointer. */
169 void     set_irg_frame (ir_graph *irg, ir_node *node);
170
171 /** Returns the node that represents the global pointer. */
172 ir_node *get_irg_globals (const ir_graph *irg);
173 /** Sets the node that represents the global pointer. */
174 void     set_irg_globals (ir_graph *irg, ir_node *node);
175
176 /** Returns the node that represents the initial memory. */
177 ir_node *get_irg_initial_mem (const ir_graph *irg);
178 /** Sets the node that represents the initial memory. */
179 void     set_irg_initial_mem (ir_graph *irg, ir_node *node);
180
181 /** Returns the node that represents the argument pointer. */
182 ir_node *get_irg_args (const ir_graph *irg);
183 /** Sets the node that represents the argument pointer. */
184 void     set_irg_args (ir_graph *irg, ir_node *node);
185
186 /** Returns the current block of a graph. */
187 ir_node *get_irg_current_block (const ir_graph *irg);
188 /** Sets the current block of a graph. */
189 void     set_irg_current_block (ir_graph *irg, ir_node *node);
190
191 /** Returns the Bad node.  Use new_Bad() instead!! */
192 ir_node *get_irg_bad (const ir_graph *irg);
193 void     set_irg_bad (ir_graph *irg, ir_node *node);
194
195 /** Returns the NoMem node.  Use new_NoMem() instead!! */
196 ir_node *get_irg_no_mem (const ir_graph *irg);
197 void     set_irg_no_mem (ir_graph *irg, ir_node *node);
198
199 /** Returns the number of value numbers of a graph. */
200 int      get_irg_n_locs (ir_graph *irg);
201
202 /** Returns the graph number. */
203 long     get_irg_graph_nr(ir_graph *irg);
204
205 /********************************************************************************/
206 /* States of an ir_graph.                                                       */
207 /********************************************************************************/
208
209 /*
210    information associated with the graph.  Optimizations invalidate these
211    states.  */
212
213 /** The states of an ir graph.
214  *
215  * state phase values: phase_building, phase_high, phase_low.
216  *
217  * The graph is in phase_building during construction of the irgraph.
218  * The construction is finished by a call to finalize_cons().
219  *
220  * Finalize_cons() sets the state to phase_high.  All Firm nodes are
221  * allowed.
222  *
223  * To get the irgraph into phase_low all Sel nodes must be removed and
224  * replaced by explicit address computations.  SymConst size and
225  * typetag nodes must be removed (@@@ really?).  Initialization of
226  * memory allocated by Alloc must be explicit.  @@@ More conditions?
227  *
228  */
229 typedef enum {
230   phase_building,
231   phase_high,
232   phase_low
233 } irg_phase_state;
234
235 irg_phase_state get_irg_phase_state (const ir_graph *irg);
236 void set_irg_phase_low(ir_graph *irg);
237
238 /** state: op_pin_state_pinned
239    The graph is "op_pin_state_pinned" if all nodes are associated with a basic block.
240    It is in state "op_pin_state_floats" if nodes are in arbitrary blocks.  In state
241    "op_pin_state_floats" the block predecessor is set in all nodes, but this can be an
242    invalid block, i.e., the block is not a dominator of all the uses of
243    the node.
244    The enum op_pin_state is defined in irop.h. */
245 op_pin_state get_irg_pinned (const ir_graph *irg);
246
247 /** state: outs_state
248    Outs are the back edges or def-use edges.
249    Values:  outs_none, outs_consistent, outs_inconsistent
250    outs_none: outs are not computed, no memory is allocated.
251    outs_consistent:  outs are computed and correct,
252    outs_inconsistent: outs have been computed, memory is still allocated,
253    but the graph has been changed since. */
254 typedef enum {
255   outs_none,
256   outs_consistent,
257   outs_inconsistent
258 } irg_outs_state;
259 irg_outs_state get_irg_outs_state(const ir_graph *irg);
260 void set_irg_outs_inconsistent(ir_graph *irg);
261
262 /** state: dom_state
263    Signals the state of the dominator infomation.
264    Values:  dom_none, dom_consistent, dom_inconsistent
265    dom_none: doms are not computed, no memory is allocated.  The access routines
266    may not be used.
267    dom_consistent:  dominator information is computed and correct,
268    dom_inconsistent: dominator information is computed, memory is still allocated,
269    but the graph has been changed since. Using the access routines is possible,
270    obtained information may be incorrect. */
271 typedef enum {
272   dom_none,             /**< doms are not computed, no memory is allocated */
273   dom_consistent,       /**< dominator information is computed and correct */
274   dom_inconsistent      /**<  dominator information is computed but the graph has been changed since */
275 } irg_dom_state;
276 irg_dom_state get_irg_dom_state(const ir_graph *irg);
277 void set_irg_dom_inconsistent(ir_graph *irg);
278
279 /** state: loopinfo_state
280    Loop information describes the loops within the control and
281    data flow of the procedure.  */
282 typedef enum {
283   loopinfo_none             = 0,       /**< No loop information is constructed. Default. */
284   loopinfo_constructed      = 1,       /**< Some kind of loop information is constructed. */
285   loopinfo_valid            = 2,       /**< Loop information is valid. */
286   loopinfo_cf               = 4,       /**< Loop information constructed for control flow only. */
287   loopinfo_inter            = 8,       /**< Loop information for interprocedural view. */
288
289   loopinfo_for_firmjni      = 16,      /**< A hack for firmjni:  all enums must differ as they
290                                           are used in a switch. */
291
292   /** IntRAprocedural loop information constructed and valid. */
293   loopinfo_consistent         = loopinfo_constructed | loopinfo_valid,
294   /** IntRAprocedural loop information constructed and invalid. */
295   loopinfo_inconsistent       = loopinfo_constructed | loopinfo_for_firmjni,
296
297   /** IntERprocedural loop information constructed and valid. */
298   loopinfo_ip_consistent      = loopinfo_constructed | loopinfo_inter | loopinfo_valid,
299   /** IntERprocedural loop information constructed and invalid. */
300   loopinfo_ip_inconsistent    = loopinfo_constructed | loopinfo_inter,
301
302   /** IntRAprocedural control loop information constructed and valid. */
303   loopinfo_cf_consistent      = loopinfo_constructed | loopinfo_cf | loopinfo_valid,
304   /** IntRAprocedural control loop information constructed and invalid. */
305   loopinfo_cf_inconsistent    = loopinfo_constructed | loopinfo_cf,
306
307   /** IntERprocedural control loop information constructed and valid. */
308   loopinfo_cf_ip_consistent   = loopinfo_constructed | loopinfo_cf | loopinfo_inter | loopinfo_valid,
309   /** IntERprocedural control loop information constructed and invalid. */
310   loopinfo_cf_ip_inconsistent = loopinfo_constructed | loopinfo_cf | loopinfo_inter
311 } irg_loopinfo_state;
312
313 irg_loopinfo_state get_irg_loopinfo_state(const ir_graph *irg);
314 void set_irg_loopinfo_state(ir_graph *irg, irg_loopinfo_state s);
315 /** Sets the loopinformation state to the appropriate inconsistent state.
316    If state is 'none' does not change. */
317 void set_irg_loopinfo_inconsistent(ir_graph *irg);
318
319 /** state: callee_information_state
320  *  Call nodes contain a list of possible callees.  This list must be
321  *  computed by an analysis.
322  *
323  *  It's strange that this state is administered on irg basis, as the
324  *  information must be computed for the whole program, or not?
325  */
326 typedef enum {
327   irg_callee_info_none,
328   irg_callee_info_consistent,
329   irg_callee_info_inconsistent
330 } irg_callee_info_state;
331 irg_callee_info_state get_irg_callee_info_state(const ir_graph *irg);
332 void                  set_irg_callee_info_state(ir_graph *irg, irg_callee_info_state s);
333
334 /** property:
335  *  Tells how to handle an ir graph in inlineing.
336  */
337 typedef enum {
338   irg_inline_any,         /**< No restriction on inlineing. Default. */
339   irg_inline_forbidden,   /**< The graph may not be inlined. */
340   irg_inline_recomended,  /**< The graph should be inlined. */
341   irg_inline_forced       /**< The graph must be inlined. */
342 } irg_inline_property;
343
344 /** Returns the inline property of a graph. */
345 irg_inline_property get_irg_inline_property(const ir_graph *irg);
346 /** Sets the inline property of a graph. */
347 void set_irg_inline_property(ir_graph *irg, irg_inline_property s);
348
349 /** A void * field to link arbritary information to the node. */
350 void  set_irg_link (ir_graph *irg, void *thing);
351 void *get_irg_link (const ir_graph *irg);
352
353 /* increments visited by one */
354 void          inc_irg_visited (ir_graph *irg);
355 unsigned long get_irg_visited (const ir_graph *irg);
356 void          set_irg_visited (ir_graph *irg, unsigned long i);
357 unsigned long get_max_irg_visited (void);
358 void          set_max_irg_visited (int val);
359 unsigned long inc_max_irg_visited (void);
360
361 /* increments block_visited by one */
362 void          inc_irg_block_visited (ir_graph *irg);
363 unsigned long get_irg_block_visited (const ir_graph *irg);
364 void          set_irg_block_visited (ir_graph *irg, unsigned long i);
365
366 # endif /* _IRGRAPH_H_ */