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
9 * Copyright: (c) 1998-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
16 * ir graph construction.
18 * @author Martin Trapp, Christian Schaefer
25 #include "firm_types.h"
28 #include "firm_common_t.h"
29 #include "irtypeinfo.h"
31 #include "pseudo_irg.h"
35 #include "tr_inheritance.h"
38 #include "execution_frequency.h"
44 #define FRAME_TP_SUFFIX "frame_tp"
47 * Edge info to put into an irg.
49 typedef struct _irg_edge_info_t {
51 unsigned activated : 1;
55 /** ir_graph holds all information for a procedure */
57 firm_kind kind; /**< always set to k_ir_graph*/
58 /* -- Basics of the representation -- */
59 struct entity *ent; /**< The entity of this procedure, i.e.,
60 the type of the procedure and the
61 class it belongs to. */
62 ir_type *frame_type; /**< A class type representing the stack frame.
63 Can include "inner" methods. */
64 ir_node *start_block; /**< block the start node will belong to */
65 ir_node *start; /**< start node of this ir_graph */
66 ir_node *end_block; /**< block the end node will belong to */
67 ir_node *end; /**< end node of this ir_graph */
68 ir_node *end_reg; /**< end node of this ir_graph */
69 ir_node *end_except; /**< end node of this ir_graph */
70 ir_node *cstore; /**< constant store -- no more needed!! */
71 ir_node *frame; /**< method's frame */
72 ir_node *globals; /**< pointer to the data segment containing all
73 globals as well as global procedures. */
74 ir_node *initial_mem; /**< initial memory of this graph */
75 ir_node *args; /**< methods arguments */
76 ir_node **proj_args; /**< projs of the methods arguments */
77 ir_node *bad; /**< bad node of this ir_graph, the one and
79 ir_node *no_mem; /**< NoMem node of this ir_graph, the one and
81 /* GL removed: we need unknown with mode for analyses. */
82 /* struct ir_node *unknown;*/ /**< unknown node of this ir_graph */
83 struct obstack *obst; /**< obstack where all of the ir_nodes live */
84 ir_node *current_block; /**< block for newly gen_*()-erated ir_nodes */
85 struct obstack *extbb_obst; /**< obstack for extended basic block info */
87 /* -- Fields for graph properties -- */
88 irg_inline_property inline_property; /**< How to handle inlineing. */
89 unsigned additional_properties; /**< additional graph properties. */
91 /* -- Fields indicating different states of irgraph -- */
92 irg_phase_state phase_state; /**< compiler phase */
93 op_pin_state irg_pinned_state; /**< Flag for status of nodes */
94 irg_outs_state outs_state; /**< Out edges. */
95 irg_dom_state dom_state; /**< Dominator information */
96 ir_typeinfo_state typeinfo_state; /**< Validity of type information */
97 irg_callee_info_state callee_info_state; /**< Validity of callee information */
98 irg_loopinfo_state loopinfo_state; /**< state of loop information */
99 exec_freq_state execfreq_state; /**< state of execution frequency information */
100 ir_class_cast_state class_cast_state; /**< kind of cast operations in code. */
101 irg_extblk_info_state extblk_state; /**< state of extended basic block info */
103 /* -- Fields for construction -- */
104 #if USE_EXPLICIT_PHI_IN_STACK
105 struct Phi_in_stack *Phi_in_stack; /**< needed for automatic Phi construction */
107 int n_loc; /**< number of local variable in this
108 procedure including procedure parameters. */
109 void **loc_descriptions; /**< storage for local variable desriptions */
111 /* -- Fields for optimizations / analysis information -- */
112 pset *value_table; /**< hash table for global value numbering (cse)
113 for optimizing use in iropt.c */
114 struct ir_node **outs; /**< Space for the out arrays. */
117 int n_outs; /**< Size wasted for outs */
118 #endif /* defined DEBUG_libfirm */
119 struct ir_loop *loop; /**< The outermost loop */
120 void *link; /**< A void* field to link any information to
123 ir_graph **callers; /**< For callgraph analysis. */
124 unsigned char *caller_isbe; /**< For callgraph analysis: set if backedge. */
125 ir_graph **callees; /**< For callgraph analysis. */
126 unsigned char *callee_isbe; /**< For callgraph analysis: set if backedge. */
127 int callgraph_loop_depth; /**< For callgraph analysis */
128 int callgraph_recursion_depth; /**< For callgraph analysis */
129 double method_execution_frequency; /**< For callgraph analysis */
133 /* -- Fields for Walking the graph -- */
134 unsigned long visited; /**< this flag is an identifier for
135 ir walk. it will be incremented
136 every time someone walks through
138 unsigned long block_visited; /**< same as visited, for a complete block */
139 unsigned estimated_node_count; /**< estimated number of nodes in this graph,
140 updated after every walk */
141 #if FIRM_EDGES_INPLACE
142 irg_edge_info_t edge_info; /**< edge info for automatic outs */
145 int graph_nr; /**< a unique graph number for each graph to make output
152 * Initializes the graph construction module
154 void firm_init_irgraph(void);
156 /* Internal constructor that does not add to irp_irgs or the like. */
157 ir_graph *new_r_ir_graph (entity *ent, int n_loc);
159 /** Make a rudimentary ir graph for the constant code.
160 Must look like a correct irg, spare everything else. */
161 ir_graph *new_const_code_irg(void);
164 * Set the op_pin_state_pinned state of a graph.
166 * @param irg the IR graph
167 * @param p new pin state
170 set_irg_pinned (ir_graph *irg, op_pin_state p);
172 /** Returns the obstack associated with the graph. */
173 struct obstack *get_irg_obstack(const ir_graph *irg);
176 * Returns true if the node n is allocated on the storage of graph irg.
178 * @param irg the IR graph
179 * @param n the IR node
181 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n);
183 /*-------------------------------------------------------------------*/
184 /* inline functions for graphs */
185 /*-------------------------------------------------------------------*/
187 extern int firm_interprocedural_view;
190 _get_interprocedural_view(void) {
191 return firm_interprocedural_view;
195 _is_ir_graph(const void *thing) {
196 return (get_kind(thing) == k_ir_graph);
199 /** Returns the start block of a graph. */
200 static INLINE ir_node *
201 _get_irg_start_block(const ir_graph *irg) {
202 return irg->start_block;
206 _set_irg_start_block(ir_graph *irg, ir_node *node) {
207 irg->start_block = node;
210 static INLINE ir_node *
211 _get_irg_start(const ir_graph *irg) {
216 _set_irg_start(ir_graph *irg, ir_node *node) {
220 static INLINE ir_node *
221 _get_irg_end_block(const ir_graph *irg) {
222 return irg->end_block;
226 _set_irg_end_block(ir_graph *irg, ir_node *node) {
227 irg->end_block = node;
230 static INLINE ir_node *
231 _get_irg_end(const ir_graph *irg) {
236 _set_irg_end(ir_graph *irg, ir_node *node) {
240 static INLINE ir_node *
241 _get_irg_end_reg(const ir_graph *irg) {
245 static INLINE ir_node *
246 _get_irg_end_except (const ir_graph *irg) {
247 return irg->end_except;
250 static INLINE ir_node *
251 _get_irg_cstore(const ir_graph *irg) {
256 _set_irg_cstore(ir_graph *irg, ir_node *node) {
260 static INLINE ir_node *
261 _get_irg_frame(const ir_graph *irg) {
266 _set_irg_frame(ir_graph *irg, ir_node *node) {
270 static INLINE ir_node *
271 _get_irg_globals(const ir_graph *irg) {
276 _set_irg_globals(ir_graph *irg, ir_node *node) {
280 static INLINE ir_node *
281 _get_irg_initial_mem(const ir_graph *irg) {
282 return irg->initial_mem;
286 _set_irg_initial_mem(ir_graph *irg, ir_node *node) {
287 irg->initial_mem = node;
290 static INLINE ir_node *
291 _get_irg_args(const ir_graph *irg) {
296 _set_irg_args(ir_graph *irg, ir_node *node) {
300 static INLINE ir_node **
301 _get_irg_proj_args(const ir_graph *irg) {
302 return irg->proj_args;
306 _set_irg_proj_args(ir_graph *irg, ir_node **nodes) {
307 irg->proj_args = nodes;
310 static INLINE ir_node *
311 _get_irg_bad(const ir_graph *irg) {
316 _set_irg_bad(ir_graph *irg, ir_node *node) {
320 static INLINE ir_node *
321 _get_irg_no_mem(const ir_graph *irg) {
326 _set_irg_no_mem(ir_graph *irg, ir_node *node) {
329 static INLINE ir_node *
330 _get_irg_current_block(const ir_graph *irg) {
331 return irg->current_block;
335 _set_irg_current_block(ir_graph *irg, ir_node *node) {
336 irg->current_block = node;
339 static INLINE entity *
340 _get_irg_entity(const ir_graph *irg) {
341 assert(irg && irg->ent);
346 _set_irg_entity(ir_graph *irg, entity *ent) {
351 _get_irg_frame_type(ir_graph *irg) {
352 assert(irg && irg->frame_type);
353 return irg->frame_type = skip_tid(irg->frame_type);
357 _set_irg_frame_type(ir_graph *irg, type *ftp) {
358 assert(is_Class_type(ftp));
359 irg->frame_type = ftp;
362 static INLINE struct obstack *
363 _get_irg_obstack(const ir_graph *irg) {
368 static INLINE irg_phase_state
369 _get_irg_phase_state(const ir_graph *irg) {
370 return irg->phase_state;
374 _set_irg_phase_low(ir_graph *irg) {
375 irg->phase_state = phase_low;
378 static INLINE op_pin_state
379 _get_irg_pinned(const ir_graph *irg) {
380 return irg->irg_pinned_state;
383 static INLINE irg_outs_state
384 _get_irg_outs_state(const ir_graph *irg) {
385 return irg->outs_state;
389 _set_irg_outs_inconsistent(ir_graph *irg) {
390 if (irg->outs_state == outs_consistent)
391 irg->outs_state = outs_inconsistent;
394 static INLINE irg_dom_state
395 _get_irg_dom_state(const ir_graph *irg) {
396 return irg->dom_state;
400 _set_irg_dom_inconsistent(ir_graph *irg) {
401 irg->dom_state = dom_inconsistent;
404 static INLINE irg_loopinfo_state
405 _get_irg_loopinfo_state(const ir_graph *irg) {
406 return irg->loopinfo_state;
410 _set_irg_loopinfo_state(ir_graph *irg, irg_loopinfo_state s) {
411 irg->loopinfo_state = s;
415 _set_irg_loopinfo_inconsistent(ir_graph *irg) {
416 irg->loopinfo_state &= ~loopinfo_valid;
420 _set_irg_pinned(ir_graph *irg, op_pin_state p) {
421 irg->irg_pinned_state = p;
424 static INLINE irg_callee_info_state
425 _get_irg_callee_info_state(const ir_graph *irg) {
426 return irg->callee_info_state;
430 _set_irg_callee_info_state(ir_graph *irg, irg_callee_info_state s) {
431 irg_callee_info_state irp_state = get_irp_callee_info_state();
433 irg->callee_info_state = s;
435 /* I could compare ... but who knows? */
436 if ((irp_state == irg_callee_info_consistent) ||
437 ((irp_state == irg_callee_info_inconsistent) && (s == irg_callee_info_none)))
438 set_irp_callee_info_state(s);
441 static INLINE irg_inline_property
442 _get_irg_inline_property(const ir_graph *irg) {
443 return irg->inline_property;
447 _set_irg_inline_property(ir_graph *irg, irg_inline_property s) {
448 irg->inline_property = s;
451 static INLINE unsigned
452 _get_irg_additional_properties(const ir_graph *irg) {
453 if (irg->additional_properties & mtp_property_inherited)
454 return get_method_additional_properties(get_entity_type(irg->ent));
455 return irg->additional_properties;
459 _set_irg_additional_properties(ir_graph *irg, unsigned mask) {
460 /* do not allow to set the mtp_property_inherited flag or
461 * the automatic inheritance of flags will not work */
462 irg->additional_properties = mask & ~mtp_property_inherited;
466 _set_irg_additional_property(ir_graph *irg, mtp_additional_property flag) {
467 unsigned prop = irg->additional_properties;
469 if (prop & mtp_property_inherited)
470 prop = get_method_additional_properties(get_entity_type(irg->ent));
471 irg->additional_properties = prop | flag;
475 _set_irg_link(ir_graph *irg, void *thing) {
480 _get_irg_link(const ir_graph *irg) {
484 static INLINE unsigned long
485 _get_irg_visited(const ir_graph *irg) {
489 static INLINE unsigned long
490 _get_irg_block_visited(const ir_graph *irg) {
491 return irg->block_visited;
495 _set_irg_block_visited(ir_graph *irg, unsigned long visited) {
496 irg->block_visited = visited;
500 _inc_irg_block_visited(ir_graph *irg) {
501 ++irg->block_visited;
504 static INLINE unsigned
505 _get_irg_estimated_node_cnt(const ir_graph *irg) {
506 return irg->estimated_node_count;
509 #define get_interprocedural_view() _get_interprocedural_view()
510 #define is_ir_graph(thing) _is_ir_graph(thing)
511 #define get_irg_start_block(irg) _get_irg_start_block(irg)
512 #define set_irg_start_block(irg, node) _set_irg_start_block(irg, node)
513 #define get_irg_start(irg) _get_irg_start(irg)
514 #define set_irg_start(irg, node) _set_irg_start(irg, node)
515 #define get_irg_end_block(irg) _get_irg_end_block(irg)
516 #define set_irg_end_block(irg, node) _set_irg_end_block(irg, node)
517 #define get_irg_end(irg) _get_irg_end(irg)
518 #define set_irg_end(irg, node) _set_irg_end(irg, node)
519 #define get_irg_end_reg(irg) _get_irg_end_reg(irg)
520 #define get_irg_end_except(irg) _get_irg_end_except(irg)
521 #define get_irg_cstore(irg) _get_irg_cstore(irg)
522 #define set_irg_cstore(irg, node) _set_irg_cstore(irg, node)
523 #define get_irg_frame(irg) _get_irg_frame(irg)
524 #define set_irg_frame(irg, node) _set_irg_frame(irg, node)
525 #define get_irg_globals(irg) _get_irg_globals(irg)
526 #define set_irg_globals(irg, node) _set_irg_globals(irg, node)
527 #define get_irg_initial_mem(irg) _get_irg_initial_mem(irg)
528 #define set_irg_initial_mem(irg, node) _set_irg_initial_mem(irg, node)
529 #define get_irg_args(irg) _get_irg_args(irg)
530 #define set_irg_args(irg, node) _set_irg_args(irg, node)
531 #define get_irg_bad(irg) _get_irg_bad(irg)
532 #define set_irg_bad(irg, node) _set_irg_bad(irg, node)
533 #define get_irg_no_mem(irg) _get_irg_no_mem(irg)
534 #define set_irg_no_mem(irg, node) _set_irg_no_mem(irg, node)
535 #define get_irg_current_block(irg) _get_irg_current_block(irg)
536 #define set_irg_current_block(irg, node) _set_irg_current_block(irg, node)
537 #define get_irg_entity(irg) _get_irg_entity(irg)
538 #define set_irg_entity(irg, ent) _set_irg_entity(irg, ent)
539 #define get_irg_frame_type(irg) _get_irg_frame_type(irg)
540 #define set_irg_frame_type(irg, ftp) _set_irg_frame_type(irg, ftp)
541 #define get_irg_obstack(irg) _get_irg_obstack(irg)
542 #define get_irg_phase_state(irg) _get_irg_phase_state(irg)
543 #define set_irg_phase_low(irg) _set_irg_phase_low(irg)
544 #define get_irg_pinned(irg) _get_irg_pinned(irg)
545 #define get_irg_outs_state(irg) _get_irg_outs_state(irg)
546 #define set_irg_outs_inconsistent(irg) _set_irg_outs_inconsistent(irg)
547 #define get_irg_dom_state(irg) _get_irg_dom_state(irg)
548 #define set_irg_dom_inconsistent(irg) _set_irg_dom_inconsistent(irg)
549 #define get_irg_loopinfo_state(irg) _get_irg_loopinfo_state(irg)
550 #define set_irg_loopinfo_state(irg, s) _set_irg_loopinfo_state(irg, s)
551 #define set_irg_loopinfo_inconsistent(irg) _set_irg_loopinfo_inconsistent(irg)
552 #define set_irg_pinned(irg, p) _set_irg_pinned(irg, p)
553 #define get_irg_callee_info_state(irg) _get_irg_callee_info_state(irg)
554 #define set_irg_callee_info_state(irg, s) _set_irg_callee_info_state(irg, s)
555 #define get_irg_inline_property(irg) _get_irg_inline_property(irg)
556 #define set_irg_inline_property(irg, s) _set_irg_inline_property(irg, s)
557 #define get_irg_additional_properties(irg) _get_irg_additional_properties(irg)
558 #define set_irg_additional_properties(irg, m) _set_irg_additional_properties(irg, m)
559 #define set_irg_additional_property(irg, f) _set_irg_additional_property(irg, f)
560 #define set_irg_link(irg, thing) _set_irg_link(irg, thing)
561 #define get_irg_link(irg) _get_irg_link(irg)
562 #define get_irg_visited(irg) _get_irg_visited(irg)
563 #define get_irg_block_visited(irg) _get_irg_block_visited(irg)
564 #define set_irg_block_visited(irg, v) _set_irg_block_visited(irg, v)
565 #define inc_irg_block_visited(irg) _inc_irg_block_visited(irg)
566 #define get_irg_estimated_node_cnt(irg) _get_irg_estimated_node_cnt(irg)
568 # endif /* _IRGRAPH_T_H_ */