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