removed the INPLACE_EDGES option. They are now always available
[libfirm] / ir / ir / irgraph_t.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irgraph.c
4  * Purpose:     Entry point to the representation of procedure code -- internal header.
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_t.h
15  *
16  * ir graph construction.
17  *
18  * @author Martin Trapp, Christian Schaefer
19  */
20
21
22 #ifndef _IRGRAPH_T_H_
23 #define _IRGRAPH_T_H_
24
25 #include "firm_types.h"
26 #include "irgraph.h"
27
28 #include "firm_common_t.h"
29 #include "irtypeinfo.h"
30 #include "irprog.h"
31 #include "pseudo_irg.h"
32 #include "type_t.h"
33 #include "entity_t.h"
34 #include "typegmod.h"
35 #include "tr_inheritance.h"
36
37 #include "irloop.h"
38 #include "execution_frequency.h"
39
40 #include "obst.h"
41 #include "pset.h"
42 #include "set.h"
43
44 #define FRAME_TP_SUFFIX "frame_tp"
45
46 /**
47  * Edge info to put into an irg.
48  */
49 typedef struct _irg_edge_info_t {
50         set *edges;
51         unsigned activated : 1;
52 } irg_edge_info_t;
53
54 /**
55  * Index constants for nodes that can be accessed through the graph itself.
56  */
57 enum irg_anchors {
58   anchor_start_block = 0, /**< block the start node will belong to */
59   anchor_start,           /**< start node of this ir_graph */
60   anchor_end_block,       /**< block the end node will belong to */
61   anchor_end,             /**< end node of this ir_graph */
62   anchor_end_reg,         /**< end node of this ir_graph */
63   anchor_end_except,      /**< end node of this ir_graph */
64   anchor_frame,           /**< method's frame */
65   anchor_globals,         /**< pointer to the data segment containing all
66                                globals as well as global procedures. */
67   anchor_initial_mem,     /**< initial memory of this graph */
68   anchor_args,            /**< methods arguments */
69   anchor_bad,             /**< bad node of this ir_graph, the one and
70                                only in this graph */
71   anchor_no_mem,          /**< NoMem node of this ir_graph, the one and only in this graph */
72   anchor_max
73 };
74
75 /** ir_graph holds all information for a procedure */
76 struct ir_graph {
77   firm_kind         kind;            /**<  always set to k_ir_graph*/
78   /* --  Basics of the representation -- */
79   entity  *ent;           /**< The entity of this procedure, i.e.,
80                     the type of the procedure and the
81                     class it belongs to. */
82   ir_type *frame_type;           /**< A class type representing the stack frame.
83                                       Can include "inner" methods. */
84   ir_node *anchors[anchor_max];  /**< anchor nodes */
85   ir_node **proj_args;           /**< projs of the methods arguments */
86   struct obstack *obst;          /**< obstack where all of the ir_nodes live */
87   ir_node *current_block;        /**< block for newly gen_*()-erated ir_nodes */
88   struct obstack *extbb_obst;    /**< obstack for extended basic block info */
89
90   unsigned last_node_idx;        /**< last IR node index for this graph */
91
92   /* -- Fields for graph properties -- */
93   irg_inline_property inline_property;     /**< How to handle inlineing. */
94   unsigned additional_properties;          /**< additional graph properties. */
95
96   /* -- Fields indicating different states of irgraph -- */
97   irg_phase_state phase_state;       /**< compiler phase */
98   op_pin_state irg_pinned_state;     /**< Flag for status of nodes */
99   irg_outs_state outs_state;         /**< Out edges. */
100   irg_dom_state dom_state;           /**< Dominator state information */
101   irg_dom_state pdom_state;          /**< Post Dominator state information */
102   ir_typeinfo_state typeinfo_state;        /**< Validity of type information */
103   irg_callee_info_state callee_info_state; /**< Validity of callee information */
104   irg_loopinfo_state loopinfo_state;       /**< state of loop information */
105   exec_freq_state   execfreq_state;        /**< state of execution frequency information */
106   ir_class_cast_state class_cast_state;    /**< kind of cast operations in code. */
107   irg_extblk_info_state extblk_state;      /**< state of extended basic block info */
108
109   /* -- Fields for construction -- */
110 #if USE_EXPLICIT_PHI_IN_STACK
111   struct Phi_in_stack *Phi_in_stack; /**< needed for automatic Phi construction */
112 #endif
113   int n_loc;                         /**< number of local variable in this
114                                           procedure including procedure parameters. */
115   void **loc_descriptions;           /**< storage for local variable descriptions */
116
117   /* -- Fields for optimizations / analysis information -- */
118   pset *value_table;                 /**< hash table for global value numbering (cse)
119                     for optimizing use in iropt.c */
120   ir_node **outs;                    /**< Space for the out arrays. */
121
122   ir_loop *loop;                     /**< The outermost loop */
123   void *link;                        /**< A void* field to link any information to
124                     the node. */
125
126   ir_graph **callers;                /**< For callgraph analysis. */
127   unsigned char *caller_isbe;        /**< For callgraph analysis: set if backedge. */
128   ir_graph **callees;                /**< For callgraph analysis. */
129   unsigned char *callee_isbe;        /**< For callgraph analysis: set if backedge. */
130   int        callgraph_loop_depth;         /**< For callgraph analysis */
131   int        callgraph_recursion_depth;    /**< For callgraph analysis */
132   double     method_execution_frequency;   /**< For callgraph analysis */
133
134   ir_loop   *l;
135
136   /* -- Fields for Walking the graph -- */
137   unsigned long visited;             /**< this flag is an identifier for
138                     ir walk. it will be incremented
139                     every time someone walks through
140                     the graph */
141   unsigned long block_visited;       /**< same as visited, for a complete block */
142   unsigned estimated_node_count;     /**< estimated number of nodes in this graph,
143                                           updated after every walk */
144   irg_edge_info_t edge_info;  /**< edge info for automatic outs */
145
146 #ifdef DEBUG_libfirm
147   int             n_outs;            /**< Size wasted for outs */
148   long graph_nr;              /**< a unique graph number for each graph to make output
149                    readable. */
150 #endif
151
152 };
153
154 /**
155  * Initializes the graph construction module
156  */
157 void firm_init_irgraph(void);
158
159 /* Internal constructor that does not add to irp_irgs or the like. */
160 ir_graph *new_r_ir_graph (entity *ent, int n_loc);
161
162 /** Make a rudimentary ir graph for the constant code.
163    Must look like a correct irg, spare everything else. */
164 ir_graph *new_const_code_irg(void);
165
166 /**
167  * Set the op_pin_state_pinned state of a graph.
168  *
169  * @param irg     the IR graph
170  * @param p       new pin state
171  */
172 INLINE void
173 set_irg_pinned (ir_graph *irg, op_pin_state p);
174
175 /** Returns the obstack associated with the graph. */
176 struct obstack *get_irg_obstack(const ir_graph *irg);
177
178 /**
179  * Returns true if the node n is allocated on the storage of graph irg.
180  *
181  * @param irg   the IR graph
182  * @param n the IR node
183  */
184 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n);
185
186 /*-------------------------------------------------------------------*/
187 /* inline functions for graphs                                       */
188 /*-------------------------------------------------------------------*/
189
190 extern int firm_interprocedural_view;
191
192 static INLINE int
193 _get_interprocedural_view(void) {
194   return firm_interprocedural_view;
195 }
196
197 static INLINE int
198 _is_ir_graph(const void *thing) {
199   return (get_kind(thing) == k_ir_graph);
200 }
201
202 /** Returns the start block of a graph. */
203 static INLINE ir_node *
204 _get_irg_start_block(const ir_graph *irg) {
205   return irg->anchors[anchor_start_block];
206 }
207
208 static INLINE void
209 _set_irg_start_block(ir_graph *irg, ir_node *node) {
210   irg->anchors[anchor_start_block] = node;
211 }
212
213 static INLINE ir_node *
214 _get_irg_start(const ir_graph *irg) {
215   return irg->anchors[anchor_start];
216 }
217
218 static INLINE void
219 _set_irg_start(ir_graph *irg, ir_node *node) {
220   irg->anchors[anchor_start] = node;
221 }
222
223 static INLINE ir_node *
224 _get_irg_end_block(const ir_graph *irg) {
225   return irg->anchors[anchor_end_block];
226 }
227
228 static INLINE void
229 _set_irg_end_block(ir_graph *irg, ir_node *node) {
230   irg->anchors[anchor_end_block] = node;
231 }
232
233 static INLINE ir_node *
234 _get_irg_end(const ir_graph *irg) {
235   return irg->anchors[anchor_end];
236 }
237
238 static INLINE void
239 _set_irg_end(ir_graph *irg, ir_node *node) {
240   irg->anchors[anchor_end] = node;
241 }
242
243 static INLINE ir_node *
244 _get_irg_end_reg(const ir_graph *irg) {
245   return irg->anchors[anchor_end_reg];
246 }
247
248 static INLINE ir_node *
249 _get_irg_end_except (const ir_graph *irg) {
250   return irg->anchors[anchor_end_except];
251 }
252
253 static INLINE ir_node *
254 _get_irg_frame(const ir_graph *irg) {
255   return irg->anchors[anchor_frame];
256 }
257
258 static INLINE void
259 _set_irg_frame(ir_graph *irg, ir_node *node) {
260   irg->anchors[anchor_frame] = node;
261 }
262
263 static INLINE ir_node *
264 _get_irg_globals(const ir_graph *irg) {
265   return irg->anchors[anchor_globals];
266 }
267
268 static INLINE void
269 _set_irg_globals(ir_graph *irg, ir_node *node) {
270   irg->anchors[anchor_globals] = node;
271 }
272
273 static INLINE ir_node *
274 _get_irg_initial_mem(const ir_graph *irg) {
275   return irg->anchors[anchor_initial_mem];
276 }
277
278 static INLINE void
279 _set_irg_initial_mem(ir_graph *irg, ir_node *node) {
280   irg->anchors[anchor_initial_mem] = node;
281 }
282
283 static INLINE ir_node *
284 _get_irg_args(const ir_graph *irg) {
285   return irg->anchors[anchor_args];
286 }
287
288 static INLINE void
289 _set_irg_args(ir_graph *irg, ir_node *node) {
290   irg->anchors[anchor_args] = node;
291 }
292
293 static INLINE ir_node **
294 _get_irg_proj_args(const ir_graph *irg) {
295   return irg->proj_args;
296 }
297
298 static INLINE void
299 _set_irg_proj_args(ir_graph *irg, ir_node **nodes) {
300   irg->proj_args = nodes;
301 }
302
303 static INLINE ir_node *
304 _get_irg_bad(const ir_graph *irg) {
305   return irg->anchors[anchor_bad];
306 }
307
308 static INLINE void
309 _set_irg_bad(ir_graph *irg, ir_node *node) {
310   irg->anchors[anchor_bad] = node;
311 }
312
313 static INLINE ir_node *
314 _get_irg_no_mem(const ir_graph *irg) {
315   return irg->anchors[anchor_no_mem];
316 }
317
318 static INLINE void
319 _set_irg_no_mem(ir_graph *irg, ir_node *node) {
320   irg->anchors[anchor_no_mem] = node;
321 }
322 static INLINE ir_node *
323 _get_irg_current_block(const ir_graph *irg) {
324   return irg->current_block;
325 }
326
327 static INLINE void
328 _set_irg_current_block(ir_graph *irg, ir_node *node) {
329   irg->current_block = node;
330 }
331
332 static INLINE entity *
333 _get_irg_entity(const ir_graph *irg) {
334   assert(irg && irg->ent);
335   return irg->ent;
336 }
337
338 static INLINE void
339 _set_irg_entity(ir_graph *irg, entity *ent) {
340   irg->ent = ent;
341 }
342
343 static INLINE ir_type *
344 _get_irg_frame_type(ir_graph *irg) {
345   assert(irg && irg->frame_type);
346   return irg->frame_type = skip_tid(irg->frame_type);
347 }
348
349 static INLINE void
350 _set_irg_frame_type(ir_graph *irg, ir_type *ftp) {
351   assert(is_frame_type(ftp));
352   irg->frame_type = ftp;
353 }
354
355 static INLINE struct obstack *
356 _get_irg_obstack(const ir_graph *irg) {
357   return irg->obst;
358 }
359
360
361 static INLINE irg_phase_state
362 _get_irg_phase_state(const ir_graph *irg) {
363   return irg->phase_state;
364 }
365
366 static INLINE void
367 _set_irg_phase_state(ir_graph *irg, irg_phase_state state) {
368   irg->phase_state = state;
369 }
370
371 static INLINE op_pin_state
372 _get_irg_pinned(const ir_graph *irg) {
373   return irg->irg_pinned_state;
374 }
375
376 static INLINE irg_outs_state
377 _get_irg_outs_state(const ir_graph *irg) {
378   return irg->outs_state;
379 }
380
381 static INLINE void
382 _set_irg_outs_inconsistent(ir_graph *irg) {
383   if (irg->outs_state == outs_consistent)
384     irg->outs_state = outs_inconsistent;
385 }
386
387 static INLINE irg_extblk_state
388 _get_irg_extblk_state(const ir_graph *irg) {
389   return irg->extblk_state;
390 }
391
392 static INLINE void
393 _set_irg_extblk_inconsistent(ir_graph *irg) {
394   if (irg->extblk_state == extblk_valid)
395     irg->extblk_state = extblk_invalid;
396 }
397
398 static INLINE irg_dom_state
399 _get_irg_dom_state(const ir_graph *irg) {
400   return irg->dom_state;
401 }
402
403 static INLINE irg_dom_state
404 _get_irg_postdom_state(const ir_graph *irg) {
405   return irg->pdom_state;
406 }
407
408 static INLINE void
409 _set_irg_doms_inconsistent(ir_graph *irg) {
410   if (irg->dom_state != dom_none)
411     irg->dom_state = dom_inconsistent;
412   if (irg->pdom_state != dom_none)
413     irg->pdom_state = dom_inconsistent;
414 }
415
416 static INLINE irg_loopinfo_state
417 _get_irg_loopinfo_state(const ir_graph *irg) {
418   return irg->loopinfo_state;
419 }
420
421 static INLINE void
422 _set_irg_loopinfo_state(ir_graph *irg, irg_loopinfo_state s) {
423   irg->loopinfo_state = s;
424 }
425
426 static INLINE void
427 _set_irg_loopinfo_inconsistent(ir_graph *irg) {
428   irg->loopinfo_state &= ~loopinfo_valid;
429 }
430
431 static INLINE void
432 _set_irg_pinned(ir_graph *irg, op_pin_state p) {
433   irg->irg_pinned_state = p;
434 }
435
436 static INLINE irg_callee_info_state
437 _get_irg_callee_info_state(const ir_graph *irg) {
438   return irg->callee_info_state;
439 }
440
441 static INLINE void
442 _set_irg_callee_info_state(ir_graph *irg, irg_callee_info_state s) {
443   irg_callee_info_state irp_state = get_irp_callee_info_state();
444
445   irg->callee_info_state = s;
446
447   /* I could compare ... but who knows? */
448   if ((irp_state == irg_callee_info_consistent)  ||
449       ((irp_state == irg_callee_info_inconsistent) && (s == irg_callee_info_none)))
450       set_irp_callee_info_state(s);
451 }
452
453 static INLINE irg_inline_property
454 _get_irg_inline_property(const ir_graph *irg) {
455   return irg->inline_property;
456 }
457
458 static INLINE void
459 _set_irg_inline_property(ir_graph *irg, irg_inline_property s) {
460   irg->inline_property = s;
461 }
462
463 static INLINE unsigned
464 _get_irg_additional_properties(const ir_graph *irg) {
465   if (irg->additional_properties & mtp_property_inherited)
466     return get_method_additional_properties(get_entity_type(irg->ent));
467   return irg->additional_properties;
468 }
469
470 static INLINE void
471 _set_irg_additional_properties(ir_graph *irg, unsigned mask) {
472   /* do not allow to set the mtp_property_inherited flag or
473    * the automatic inheritance of flags will not work */
474   irg->additional_properties = mask & ~mtp_property_inherited;
475 }
476
477 static INLINE void
478 _set_irg_additional_property(ir_graph *irg, mtp_additional_property flag) {
479   unsigned prop = irg->additional_properties;
480
481   if (prop & mtp_property_inherited)
482     prop = get_method_additional_properties(get_entity_type(irg->ent));
483   irg->additional_properties = prop | flag;
484 }
485
486 static INLINE void
487 _set_irg_link(ir_graph *irg, void *thing) {
488   irg->link = thing;
489 }
490
491 static INLINE void *
492 _get_irg_link(const ir_graph *irg) {
493   return irg->link;
494 }
495
496 static INLINE unsigned long
497 _get_irg_visited(const ir_graph *irg) {
498   return irg->visited;
499 }
500
501 static INLINE unsigned long
502 _get_irg_block_visited(const ir_graph *irg) {
503   return irg->block_visited;
504 }
505
506 static INLINE void
507 _set_irg_block_visited(ir_graph *irg, unsigned long visited) {
508   irg->block_visited = visited;
509 }
510
511 static INLINE void
512 _inc_irg_block_visited(ir_graph *irg) {
513   ++irg->block_visited;
514 }
515
516 static INLINE void
517 _dec_irg_block_visited(ir_graph *irg) {
518   --irg->block_visited;
519 }
520
521 static INLINE unsigned
522 _get_irg_estimated_node_cnt(const ir_graph *irg) {
523   return irg->estimated_node_count;
524 }
525
526 /**
527  * Returns the next IR node index for the given graph.
528  */
529 static INLINE unsigned
530 get_irg_next_node_idx(ir_graph *irg) {
531   return irg->last_node_idx++;
532 }
533
534 /**
535  * Kill a node from the irg. BEWARE: this kills
536  * all later created nodes.
537  */
538 static INLINE void
539 irg_kill_node(ir_graph *irg, ir_node *n) {
540   if (get_irn_idx(n) + 1 == irg->last_node_idx)
541     --irg->last_node_idx;
542   obstack_free(irg->obst, n);
543 }
544
545 #define get_interprocedural_view()            _get_interprocedural_view()
546 #define is_ir_graph(thing)                    _is_ir_graph(thing)
547 #define get_irg_start_block(irg)              _get_irg_start_block(irg)
548 #define set_irg_start_block(irg, node)        _set_irg_start_block(irg, node)
549 #define get_irg_start(irg)                    _get_irg_start(irg)
550 #define set_irg_start(irg, node)              _set_irg_start(irg, node)
551 #define get_irg_end_block(irg)                _get_irg_end_block(irg)
552 #define set_irg_end_block(irg, node)          _set_irg_end_block(irg, node)
553 #define get_irg_end(irg)                      _get_irg_end(irg)
554 #define set_irg_end(irg, node)                _set_irg_end(irg, node)
555 #define get_irg_end_reg(irg)                  _get_irg_end_reg(irg)
556 #define get_irg_end_except(irg)               _get_irg_end_except(irg)
557 #define get_irg_frame(irg)                    _get_irg_frame(irg)
558 #define set_irg_frame(irg, node)              _set_irg_frame(irg, node)
559 #define get_irg_globals(irg)                  _get_irg_globals(irg)
560 #define set_irg_globals(irg, node)            _set_irg_globals(irg, node)
561 #define get_irg_initial_mem(irg)              _get_irg_initial_mem(irg)
562 #define set_irg_initial_mem(irg, node)        _set_irg_initial_mem(irg, node)
563 #define get_irg_args(irg)                     _get_irg_args(irg)
564 #define set_irg_args(irg, node)               _set_irg_args(irg, node)
565 #define get_irg_bad(irg)                      _get_irg_bad(irg)
566 #define set_irg_bad(irg, node)                _set_irg_bad(irg, node)
567 #define get_irg_no_mem(irg)                   _get_irg_no_mem(irg)
568 #define set_irg_no_mem(irg, node)             _set_irg_no_mem(irg, node)
569 #define get_irg_current_block(irg)            _get_irg_current_block(irg)
570 #define set_irg_current_block(irg, node)      _set_irg_current_block(irg, node)
571 #define get_irg_entity(irg)                   _get_irg_entity(irg)
572 #define set_irg_entity(irg, ent)              _set_irg_entity(irg, ent)
573 #define get_irg_frame_type(irg)               _get_irg_frame_type(irg)
574 #define set_irg_frame_type(irg, ftp)          _set_irg_frame_type(irg, ftp)
575 #define get_irg_obstack(irg)                  _get_irg_obstack(irg)
576 #define get_irg_phase_state(irg)              _get_irg_phase_state(irg)
577 #define set_irg_phase_state(irg, state)       _set_irg_phase_state(irg, state)
578 #define get_irg_pinned(irg)                   _get_irg_pinned(irg)
579 #define get_irg_outs_state(irg)               _get_irg_outs_state(irg)
580 #define set_irg_outs_inconsistent(irg)        _set_irg_outs_inconsistent(irg)
581 #define get_irg_extblk_state(irg)             _get_irg_extblk_state(irg)
582 #define set_irg_extblk_inconsistent(irg)      _set_irg_extblk_inconsistent(irg)
583 #define get_irg_dom_state(irg)                _get_irg_dom_state(irg)
584 #define get_irg_postdom_state(irg)            _get_irg_postdom_state(irg)
585 #define set_irg_doms_inconsistent(irg)        _set_irg_doms_inconsistent(irg)
586 #define get_irg_loopinfo_state(irg)           _get_irg_loopinfo_state(irg)
587 #define set_irg_loopinfo_state(irg, s)        _set_irg_loopinfo_state(irg, s)
588 #define set_irg_loopinfo_inconsistent(irg)    _set_irg_loopinfo_inconsistent(irg)
589 #define set_irg_pinned(irg, p)                _set_irg_pinned(irg, p)
590 #define get_irg_callee_info_state(irg)        _get_irg_callee_info_state(irg)
591 #define set_irg_callee_info_state(irg, s)     _set_irg_callee_info_state(irg, s)
592 #define get_irg_inline_property(irg)          _get_irg_inline_property(irg)
593 #define set_irg_inline_property(irg, s)       _set_irg_inline_property(irg, s)
594 #define get_irg_additional_properties(irg)    _get_irg_additional_properties(irg)
595 #define set_irg_additional_properties(irg, m) _set_irg_additional_properties(irg, m)
596 #define set_irg_additional_property(irg, f)   _set_irg_additional_property(irg, f)
597 #define set_irg_link(irg, thing)              _set_irg_link(irg, thing)
598 #define get_irg_link(irg)                     _get_irg_link(irg)
599 #define get_irg_visited(irg)                  _get_irg_visited(irg)
600 #define get_irg_block_visited(irg)            _get_irg_block_visited(irg)
601 #define set_irg_block_visited(irg, v)         _set_irg_block_visited(irg, v)
602 #define inc_irg_block_visited(irg)            _inc_irg_block_visited(irg)
603 #define dec_irg_block_visited(irg)            _dec_irg_block_visited(irg)
604 #define get_irg_estimated_node_cnt(irg)       _get_irg_estimated_node_cnt(irg)
605
606 # endif /* _IRGRAPH_T_H_ */