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 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 state information */
96 irg_dom_state pdom_state; /**< Post Dominator state information */
97 ir_typeinfo_state typeinfo_state; /**< Validity of type information */
98 irg_callee_info_state callee_info_state; /**< Validity of callee information */
99 irg_loopinfo_state loopinfo_state; /**< state of loop information */
100 exec_freq_state execfreq_state; /**< state of execution frequency information */
101 ir_class_cast_state class_cast_state; /**< kind of cast operations in code. */
102 irg_extblk_info_state extblk_state; /**< state of extended basic block info */
104 /* -- Fields for construction -- */
105 #if USE_EXPLICIT_PHI_IN_STACK
106 struct Phi_in_stack *Phi_in_stack; /**< needed for automatic Phi construction */
108 int n_loc; /**< number of local variable in this
109 procedure including procedure parameters. */
110 void **loc_descriptions; /**< storage for local variable desriptions */
112 /* -- Fields for optimizations / analysis information -- */
113 pset *value_table; /**< hash table for global value numbering (cse)
114 for optimizing use in iropt.c */
115 ir_node **outs; /**< Space for the out arrays. */
118 int n_outs; /**< Size wasted for outs */
119 #endif /* defined DEBUG_libfirm */
120 ir_loop *loop; /**< The outermost loop */
121 void *link; /**< A void* field to link any information to
124 ir_graph **callers; /**< For callgraph analysis. */
125 unsigned char *caller_isbe; /**< For callgraph analysis: set if backedge. */
126 ir_graph **callees; /**< For callgraph analysis. */
127 unsigned char *callee_isbe; /**< For callgraph analysis: set if backedge. */
128 int callgraph_loop_depth; /**< For callgraph analysis */
129 int callgraph_recursion_depth; /**< For callgraph analysis */
130 double method_execution_frequency; /**< For callgraph analysis */
134 /* -- Fields for Walking the graph -- */
135 unsigned long visited; /**< this flag is an identifier for
136 ir walk. it will be incremented
137 every time someone walks through
139 unsigned long block_visited; /**< same as visited, for a complete block */
140 unsigned estimated_node_count; /**< estimated number of nodes in this graph,
141 updated after every walk */
142 #if FIRM_EDGES_INPLACE
143 irg_edge_info_t edge_info; /**< edge info for automatic outs */
146 int graph_nr; /**< a unique graph number for each graph to make output
153 * Initializes the graph construction module
155 void firm_init_irgraph(void);
157 /* Internal constructor that does not add to irp_irgs or the like. */
158 ir_graph *new_r_ir_graph (entity *ent, int n_loc);
160 /** Make a rudimentary ir graph for the constant code.
161 Must look like a correct irg, spare everything else. */
162 ir_graph *new_const_code_irg(void);
165 * Set the op_pin_state_pinned state of a graph.
167 * @param irg the IR graph
168 * @param p new pin state
171 set_irg_pinned (ir_graph *irg, op_pin_state p);
173 /** Returns the obstack associated with the graph. */
174 struct obstack *get_irg_obstack(const ir_graph *irg);
177 * Returns true if the node n is allocated on the storage of graph irg.
179 * @param irg the IR graph
180 * @param n the IR node
182 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n);
184 /*-------------------------------------------------------------------*/
185 /* inline functions for graphs */
186 /*-------------------------------------------------------------------*/
188 extern int firm_interprocedural_view;
191 _get_interprocedural_view(void) {
192 return firm_interprocedural_view;
196 _is_ir_graph(const void *thing) {
197 return (get_kind(thing) == k_ir_graph);
200 /** Returns the start block of a graph. */
201 static INLINE ir_node *
202 _get_irg_start_block(const ir_graph *irg) {
203 return irg->start_block;
207 _set_irg_start_block(ir_graph *irg, ir_node *node) {
208 irg->start_block = node;
211 static INLINE ir_node *
212 _get_irg_start(const ir_graph *irg) {
217 _set_irg_start(ir_graph *irg, ir_node *node) {
221 static INLINE ir_node *
222 _get_irg_end_block(const ir_graph *irg) {
223 return irg->end_block;
227 _set_irg_end_block(ir_graph *irg, ir_node *node) {
228 irg->end_block = node;
231 static INLINE ir_node *
232 _get_irg_end(const ir_graph *irg) {
237 _set_irg_end(ir_graph *irg, ir_node *node) {
241 static INLINE ir_node *
242 _get_irg_end_reg(const ir_graph *irg) {
246 static INLINE ir_node *
247 _get_irg_end_except (const ir_graph *irg) {
248 return irg->end_except;
251 static INLINE ir_node *
252 _get_irg_cstore(const ir_graph *irg) {
257 _set_irg_cstore(ir_graph *irg, ir_node *node) {
261 static INLINE ir_node *
262 _get_irg_frame(const ir_graph *irg) {
267 _set_irg_frame(ir_graph *irg, ir_node *node) {
271 static INLINE ir_node *
272 _get_irg_globals(const ir_graph *irg) {
277 _set_irg_globals(ir_graph *irg, ir_node *node) {
281 static INLINE ir_node *
282 _get_irg_initial_mem(const ir_graph *irg) {
283 return irg->initial_mem;
287 _set_irg_initial_mem(ir_graph *irg, ir_node *node) {
288 irg->initial_mem = node;
291 static INLINE ir_node *
292 _get_irg_args(const ir_graph *irg) {
297 _set_irg_args(ir_graph *irg, ir_node *node) {
301 static INLINE ir_node **
302 _get_irg_proj_args(const ir_graph *irg) {
303 return irg->proj_args;
307 _set_irg_proj_args(ir_graph *irg, ir_node **nodes) {
308 irg->proj_args = nodes;
311 static INLINE ir_node *
312 _get_irg_bad(const ir_graph *irg) {
317 _set_irg_bad(ir_graph *irg, ir_node *node) {
321 static INLINE ir_node *
322 _get_irg_no_mem(const ir_graph *irg) {
327 _set_irg_no_mem(ir_graph *irg, ir_node *node) {
330 static INLINE ir_node *
331 _get_irg_current_block(const ir_graph *irg) {
332 return irg->current_block;
336 _set_irg_current_block(ir_graph *irg, ir_node *node) {
337 irg->current_block = node;
340 static INLINE entity *
341 _get_irg_entity(const ir_graph *irg) {
342 assert(irg && irg->ent);
347 _set_irg_entity(ir_graph *irg, entity *ent) {
351 static INLINE ir_type *
352 _get_irg_frame_type(ir_graph *irg) {
353 assert(irg && irg->frame_type);
354 return irg->frame_type = skip_tid(irg->frame_type);
358 _set_irg_frame_type(ir_graph *irg, ir_type *ftp) {
359 assert(is_Class_type(ftp));
360 irg->frame_type = ftp;
363 static INLINE struct obstack *
364 _get_irg_obstack(const ir_graph *irg) {
369 static INLINE irg_phase_state
370 _get_irg_phase_state(const ir_graph *irg) {
371 return irg->phase_state;
375 _set_irg_phase_low(ir_graph *irg) {
376 irg->phase_state = phase_low;
379 static INLINE op_pin_state
380 _get_irg_pinned(const ir_graph *irg) {
381 return irg->irg_pinned_state;
384 static INLINE irg_outs_state
385 _get_irg_outs_state(const ir_graph *irg) {
386 return irg->outs_state;
390 _set_irg_outs_inconsistent(ir_graph *irg) {
391 if (irg->outs_state == outs_consistent)
392 irg->outs_state = outs_inconsistent;
395 static INLINE irg_dom_state
396 _get_irg_dom_state(const ir_graph *irg) {
397 return irg->dom_state;
400 static INLINE irg_dom_state
401 _get_irg_postdom_state(const ir_graph *irg) {
402 return irg->pdom_state;
406 _set_irg_doms_inconsistent(ir_graph *irg) {
407 if (irg->dom_state != dom_none)
408 irg->dom_state = dom_inconsistent;
409 if (irg->pdom_state != dom_none)
410 irg->pdom_state = dom_inconsistent;
413 static INLINE irg_loopinfo_state
414 _get_irg_loopinfo_state(const ir_graph *irg) {
415 return irg->loopinfo_state;
419 _set_irg_loopinfo_state(ir_graph *irg, irg_loopinfo_state s) {
420 irg->loopinfo_state = s;
424 _set_irg_loopinfo_inconsistent(ir_graph *irg) {
425 irg->loopinfo_state &= ~loopinfo_valid;
429 _set_irg_pinned(ir_graph *irg, op_pin_state p) {
430 irg->irg_pinned_state = p;
433 static INLINE irg_callee_info_state
434 _get_irg_callee_info_state(const ir_graph *irg) {
435 return irg->callee_info_state;
439 _set_irg_callee_info_state(ir_graph *irg, irg_callee_info_state s) {
440 irg_callee_info_state irp_state = get_irp_callee_info_state();
442 irg->callee_info_state = s;
444 /* I could compare ... but who knows? */
445 if ((irp_state == irg_callee_info_consistent) ||
446 ((irp_state == irg_callee_info_inconsistent) && (s == irg_callee_info_none)))
447 set_irp_callee_info_state(s);
450 static INLINE irg_inline_property
451 _get_irg_inline_property(const ir_graph *irg) {
452 return irg->inline_property;
456 _set_irg_inline_property(ir_graph *irg, irg_inline_property s) {
457 irg->inline_property = s;
460 static INLINE unsigned
461 _get_irg_additional_properties(const ir_graph *irg) {
462 if (irg->additional_properties & mtp_property_inherited)
463 return get_method_additional_properties(get_entity_type(irg->ent));
464 return irg->additional_properties;
468 _set_irg_additional_properties(ir_graph *irg, unsigned mask) {
469 /* do not allow to set the mtp_property_inherited flag or
470 * the automatic inheritance of flags will not work */
471 irg->additional_properties = mask & ~mtp_property_inherited;
475 _set_irg_additional_property(ir_graph *irg, mtp_additional_property flag) {
476 unsigned prop = irg->additional_properties;
478 if (prop & mtp_property_inherited)
479 prop = get_method_additional_properties(get_entity_type(irg->ent));
480 irg->additional_properties = prop | flag;
484 _set_irg_link(ir_graph *irg, void *thing) {
489 _get_irg_link(const ir_graph *irg) {
493 static INLINE unsigned long
494 _get_irg_visited(const ir_graph *irg) {
498 static INLINE unsigned long
499 _get_irg_block_visited(const ir_graph *irg) {
500 return irg->block_visited;
504 _set_irg_block_visited(ir_graph *irg, unsigned long visited) {
505 irg->block_visited = visited;
509 _inc_irg_block_visited(ir_graph *irg) {
510 ++irg->block_visited;
513 static INLINE unsigned
514 _get_irg_estimated_node_cnt(const ir_graph *irg) {
515 return irg->estimated_node_count;
518 #define get_interprocedural_view() _get_interprocedural_view()
519 #define is_ir_graph(thing) _is_ir_graph(thing)
520 #define get_irg_start_block(irg) _get_irg_start_block(irg)
521 #define set_irg_start_block(irg, node) _set_irg_start_block(irg, node)
522 #define get_irg_start(irg) _get_irg_start(irg)
523 #define set_irg_start(irg, node) _set_irg_start(irg, node)
524 #define get_irg_end_block(irg) _get_irg_end_block(irg)
525 #define set_irg_end_block(irg, node) _set_irg_end_block(irg, node)
526 #define get_irg_end(irg) _get_irg_end(irg)
527 #define set_irg_end(irg, node) _set_irg_end(irg, node)
528 #define get_irg_end_reg(irg) _get_irg_end_reg(irg)
529 #define get_irg_end_except(irg) _get_irg_end_except(irg)
530 #define get_irg_cstore(irg) _get_irg_cstore(irg)
531 #define set_irg_cstore(irg, node) _set_irg_cstore(irg, node)
532 #define get_irg_frame(irg) _get_irg_frame(irg)
533 #define set_irg_frame(irg, node) _set_irg_frame(irg, node)
534 #define get_irg_globals(irg) _get_irg_globals(irg)
535 #define set_irg_globals(irg, node) _set_irg_globals(irg, node)
536 #define get_irg_initial_mem(irg) _get_irg_initial_mem(irg)
537 #define set_irg_initial_mem(irg, node) _set_irg_initial_mem(irg, node)
538 #define get_irg_args(irg) _get_irg_args(irg)
539 #define set_irg_args(irg, node) _set_irg_args(irg, node)
540 #define get_irg_bad(irg) _get_irg_bad(irg)
541 #define set_irg_bad(irg, node) _set_irg_bad(irg, node)
542 #define get_irg_no_mem(irg) _get_irg_no_mem(irg)
543 #define set_irg_no_mem(irg, node) _set_irg_no_mem(irg, node)
544 #define get_irg_current_block(irg) _get_irg_current_block(irg)
545 #define set_irg_current_block(irg, node) _set_irg_current_block(irg, node)
546 #define get_irg_entity(irg) _get_irg_entity(irg)
547 #define set_irg_entity(irg, ent) _set_irg_entity(irg, ent)
548 #define get_irg_frame_type(irg) _get_irg_frame_type(irg)
549 #define set_irg_frame_type(irg, ftp) _set_irg_frame_type(irg, ftp)
550 #define get_irg_obstack(irg) _get_irg_obstack(irg)
551 #define get_irg_phase_state(irg) _get_irg_phase_state(irg)
552 #define set_irg_phase_low(irg) _set_irg_phase_low(irg)
553 #define get_irg_pinned(irg) _get_irg_pinned(irg)
554 #define get_irg_outs_state(irg) _get_irg_outs_state(irg)
555 #define set_irg_outs_inconsistent(irg) _set_irg_outs_inconsistent(irg)
556 #define get_irg_dom_state(irg) _get_irg_dom_state(irg)
557 #define get_irg_postdom_state(irg) _get_irg_postdom_state(irg)
558 #define set_irg_doms_inconsistent(irg) _set_irg_doms_inconsistent(irg)
559 #define get_irg_loopinfo_state(irg) _get_irg_loopinfo_state(irg)
560 #define set_irg_loopinfo_state(irg, s) _set_irg_loopinfo_state(irg, s)
561 #define set_irg_loopinfo_inconsistent(irg) _set_irg_loopinfo_inconsistent(irg)
562 #define set_irg_pinned(irg, p) _set_irg_pinned(irg, p)
563 #define get_irg_callee_info_state(irg) _get_irg_callee_info_state(irg)
564 #define set_irg_callee_info_state(irg, s) _set_irg_callee_info_state(irg, s)
565 #define get_irg_inline_property(irg) _get_irg_inline_property(irg)
566 #define set_irg_inline_property(irg, s) _set_irg_inline_property(irg, s)
567 #define get_irg_additional_properties(irg) _get_irg_additional_properties(irg)
568 #define set_irg_additional_properties(irg, m) _set_irg_additional_properties(irg, m)
569 #define set_irg_additional_property(irg, f) _set_irg_additional_property(irg, f)
570 #define set_irg_link(irg, thing) _set_irg_link(irg, thing)
571 #define get_irg_link(irg) _get_irg_link(irg)
572 #define get_irg_visited(irg) _get_irg_visited(irg)
573 #define get_irg_block_visited(irg) _get_irg_block_visited(irg)
574 #define set_irg_block_visited(irg, v) _set_irg_block_visited(irg, v)
575 #define inc_irg_block_visited(irg) _inc_irg_block_visited(irg)
576 #define get_irg_estimated_node_cnt(irg) _get_irg_estimated_node_cnt(irg)
578 # endif /* _IRGRAPH_T_H_ */