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
27 #include "firm_common_t.h"
28 #include "irtypeinfo.h"
30 #include "pseudo_irg.h"
34 #include "tr_inheritance.h"
37 #include "execution_frequency.h"
43 #define FRAME_TP_SUFFIX "frame_tp"
46 * Edge info to put into an irg.
48 typedef struct _irg_edge_info_t {
50 unsigned activated : 1;
54 /** ir_graph holds all information for a procedure */
56 firm_kind kind; /**< always set to k_ir_graph*/
57 /* -- Basics of the representation -- */
58 struct entity *ent; /**< The entity of this procedure, i.e.,
59 the type of the procedure and the
60 class it belongs to. */
61 struct type *frame_type; /**< A class type representing the stack frame.
62 Can include "inner" methods. */
63 struct ir_node *start_block; /**< block the start node will belong to */
64 struct ir_node *start; /**< start node of this ir_graph */
65 struct ir_node *end_block; /**< block the end node will belong to */
66 struct ir_node *end; /**< end node of this ir_graph */
67 struct ir_node *end_reg; /**< end node of this ir_graph */
68 struct ir_node *end_except; /**< end node of this ir_graph */
69 struct ir_node *cstore; /**< constant store -- no more needed!! */
70 struct ir_node *frame; /**< method's frame */
71 struct ir_node *globals; /**< pointer to the data segment containing all
72 globals as well as global procedures. */
73 struct ir_node *initial_mem; /**< initial memory of this graph */
74 struct ir_node *args; /**< methods arguments */
75 struct ir_node **proj_args; /**< projs of the methods arguments */
76 struct ir_node *bad; /**< bad node of this ir_graph, the one and
78 struct ir_node *no_mem; /**< NoMem node of this ir_graph, the one and
80 /* GL removed: we need unknown with mode for analyses. */
81 /* struct ir_node *unknown;*/ /**< unknown node of this ir_graph */
82 struct obstack *obst; /**< obstack where all of the ir_nodes live */
83 struct ir_node *current_block; /**< block for newly gen_*()-erated ir_nodes */
84 struct obstack *extbb_obst; /**< obstack for extended basic block info */
86 /* -- Fields for graph properties -- */
87 irg_inline_property inline_property; /**< How to handle inlineing. */
88 unsigned additional_properties; /**< additional graph properties. */
90 /* -- Fields indicating different states of irgraph -- */
91 irg_phase_state phase_state; /**< compiler phase */
92 op_pin_state irg_pinned_state; /**< Flag for status of nodes */
93 irg_outs_state outs_state; /**< Out edges. */
94 irg_dom_state dom_state; /**< Dominator information */
95 ir_typeinfo_state typeinfo_state; /**< Validity of type information */
96 irg_callee_info_state callee_info_state; /**< Validity of callee information */
97 irg_loopinfo_state loopinfo_state; /**< state of loop information */
98 exec_freq_state execfreq_state; /**< state of execution frequency information */
99 ir_class_cast_state class_cast_state; /**< kind of cast operations in code. */
100 irg_extblk_info_state extblk_state; /**< state of extended basic block info */
102 /* -- Fields for construction -- */
103 #if USE_EXPLICIT_PHI_IN_STACK
104 struct Phi_in_stack *Phi_in_stack; /**< needed for automatic Phi construction */
106 int n_loc; /**< number of local variable in this
107 procedure including procedure parameters. */
108 void **loc_descriptions; /**< storage for local variable desriptions */
110 /* -- Fields for optimizations / analysis information -- */
111 pset *value_table; /**< hash table for global value numbering (cse)
112 for optimizing use in iropt.c */
113 struct ir_node **outs; /**< Space for the out arrays. */
116 int n_outs; /**< Size wasted for outs */
117 #endif /* defined DEBUG_libfirm */
118 struct ir_loop *loop; /**< The outermost loop */
119 void *link; /**< A void* field to link any information to
122 ir_graph **callers; /**< For callgraph analysis. */
123 unsigned char *caller_isbe; /**< For callgraph analysis: set if backedge. */
124 ir_graph **callees; /**< For callgraph analysis. */
125 unsigned char *callee_isbe; /**< For callgraph analysis: set if backedge. */
126 int callgraph_loop_depth; /**< For callgraph analysis */
127 int callgraph_recursion_depth; /**< For callgraph analysis */
128 double method_execution_frequency; /**< For callgraph analysis */
132 /* -- Fields for Walking the graph -- */
133 unsigned long visited; /**< this flag is an identifier for
134 ir walk. it will be incremented
135 every time someone walks through
137 unsigned long block_visited; /**< same as visited, for a complete block */
138 unsigned estimated_node_count; /**< estimated number of nodes in this graph,
139 updated after every walk */
140 #if FIRM_EDGES_INPLACE
141 irg_edge_info_t edge_info; /**< edge info for automatic outs */
144 int graph_nr; /**< a unique graph number for each graph to make output
151 * Initializes the graph construction module
153 void firm_init_irgraph(void);
155 /* Internal constructor that does not add to irp_irgs or the like. */
156 ir_graph *new_r_ir_graph (entity *ent, int n_loc);
158 /** Make a rudimentary ir graph for the constant code.
159 Must look like a correct irg, spare everything else. */
160 ir_graph *new_const_code_irg(void);
163 * Set the op_pin_state_pinned state of a graph.
165 * @param irg the IR graph
166 * @param p new pin state
169 set_irg_pinned (ir_graph *irg, op_pin_state p);
171 /** Returns the obstack associated with the graph. */
172 struct obstack *get_irg_obstack(const ir_graph *irg);
175 * Returns true if the node n is allocated on the storage of graph irg.
177 * @param irg the IR graph
178 * @param n the IR node
180 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n);
182 /*-------------------------------------------------------------------*/
183 /* inline functions for graphs */
184 /*-------------------------------------------------------------------*/
186 extern int firm_interprocedural_view;
189 _get_interprocedural_view(void) {
190 return firm_interprocedural_view;
194 _is_ir_graph(const void *thing) {
195 return (get_kind(thing) == k_ir_graph);
198 /** Returns the start block of a graph. */
199 static INLINE ir_node *
200 _get_irg_start_block(const ir_graph *irg) {
201 return irg->start_block;
205 _set_irg_start_block(ir_graph *irg, ir_node *node) {
206 irg->start_block = node;
209 static INLINE ir_node *
210 _get_irg_start(const ir_graph *irg) {
215 _set_irg_start(ir_graph *irg, ir_node *node) {
219 static INLINE ir_node *
220 _get_irg_end_block(const ir_graph *irg) {
221 return irg->end_block;
225 _set_irg_end_block(ir_graph *irg, ir_node *node) {
226 irg->end_block = node;
229 static INLINE ir_node *
230 _get_irg_end(const ir_graph *irg) {
235 _set_irg_end(ir_graph *irg, ir_node *node) {
239 static INLINE ir_node *
240 _get_irg_end_reg(const ir_graph *irg) {
244 static INLINE ir_node *
245 _get_irg_end_except (const ir_graph *irg) {
246 return irg->end_except;
249 static INLINE ir_node *
250 _get_irg_cstore(const ir_graph *irg) {
255 _set_irg_cstore(ir_graph *irg, ir_node *node) {
259 static INLINE ir_node *
260 _get_irg_frame(const ir_graph *irg) {
265 _set_irg_frame(ir_graph *irg, ir_node *node) {
269 static INLINE ir_node *
270 _get_irg_globals(const ir_graph *irg) {
275 _set_irg_globals(ir_graph *irg, ir_node *node) {
279 static INLINE ir_node *
280 _get_irg_initial_mem(const ir_graph *irg) {
281 return irg->initial_mem;
285 _set_irg_initial_mem(ir_graph *irg, ir_node *node) {
286 irg->initial_mem = node;
289 static INLINE ir_node *
290 _get_irg_args(const ir_graph *irg) {
295 _set_irg_args(ir_graph *irg, ir_node *node) {
299 static INLINE ir_node **
300 _get_irg_proj_args(const ir_graph *irg) {
301 return irg->proj_args;
305 _set_irg_proj_args(ir_graph *irg, ir_node **nodes) {
306 irg->proj_args = nodes;
309 static INLINE ir_node *
310 _get_irg_bad(const ir_graph *irg) {
315 _set_irg_bad(ir_graph *irg, ir_node *node) {
319 static INLINE ir_node *
320 _get_irg_no_mem(const ir_graph *irg) {
325 _set_irg_no_mem(ir_graph *irg, ir_node *node) {
328 static INLINE ir_node *
329 _get_irg_current_block(const ir_graph *irg) {
330 return irg->current_block;
334 _set_irg_current_block(ir_graph *irg, ir_node *node) {
335 irg->current_block = node;
338 static INLINE entity *
339 _get_irg_entity(const ir_graph *irg) {
340 assert(irg && irg->ent);
345 _set_irg_entity(ir_graph *irg, entity *ent) {
350 _get_irg_frame_type(ir_graph *irg) {
351 assert(irg && irg->frame_type);
352 return irg->frame_type = skip_tid(irg->frame_type);
356 _set_irg_frame_type(ir_graph *irg, type *ftp) {
357 assert(is_Class_type(ftp));
358 irg->frame_type = ftp;
361 static INLINE struct obstack *
362 _get_irg_obstack(const ir_graph *irg) {
367 static INLINE irg_phase_state
368 _get_irg_phase_state(const ir_graph *irg) {
369 return irg->phase_state;
373 _set_irg_phase_low(ir_graph *irg) {
374 irg->phase_state = phase_low;
377 static INLINE op_pin_state
378 _get_irg_pinned(const ir_graph *irg) {
379 return irg->irg_pinned_state;
382 static INLINE irg_outs_state
383 _get_irg_outs_state(const ir_graph *irg) {
384 return irg->outs_state;
388 _set_irg_outs_inconsistent(ir_graph *irg) {
389 if (irg->outs_state == outs_consistent)
390 irg->outs_state = outs_inconsistent;
393 static INLINE irg_dom_state
394 _get_irg_dom_state(const ir_graph *irg) {
395 return irg->dom_state;
399 _set_irg_dom_inconsistent(ir_graph *irg) {
400 irg->dom_state = dom_inconsistent;
403 static INLINE irg_loopinfo_state
404 _get_irg_loopinfo_state(const ir_graph *irg) {
405 return irg->loopinfo_state;
409 _set_irg_loopinfo_state(ir_graph *irg, irg_loopinfo_state s) {
410 irg->loopinfo_state = s;
414 _set_irg_loopinfo_inconsistent(ir_graph *irg) {
415 irg->loopinfo_state &= ~loopinfo_valid;
419 _set_irg_pinned(ir_graph *irg, op_pin_state p) {
420 irg->irg_pinned_state = p;
423 static INLINE irg_callee_info_state
424 _get_irg_callee_info_state(const ir_graph *irg) {
425 return irg->callee_info_state;
429 _set_irg_callee_info_state(ir_graph *irg, irg_callee_info_state s) {
430 irg_callee_info_state irp_state = get_irp_callee_info_state();
432 irg->callee_info_state = s;
434 /* I could compare ... but who knows? */
435 if ((irp_state == irg_callee_info_consistent) ||
436 ((irp_state == irg_callee_info_inconsistent) && (s == irg_callee_info_none)))
437 set_irp_callee_info_state(s);
440 static INLINE irg_inline_property
441 _get_irg_inline_property(const ir_graph *irg) {
442 return irg->inline_property;
446 _set_irg_inline_property(ir_graph *irg, irg_inline_property s) {
447 irg->inline_property = s;
450 static INLINE unsigned
451 _get_irg_additional_properties(const ir_graph *irg) {
452 if (irg->additional_properties & mtp_property_inherited)
453 return get_method_additional_properties(get_entity_type(irg->ent));
454 return irg->additional_properties;
458 _set_irg_additional_properties(ir_graph *irg, unsigned mask) {
459 /* do not allow to set the mtp_property_inherited flag or
460 * the automatic inheritance of flags will not work */
461 irg->additional_properties = mask & ~mtp_property_inherited;
465 _set_irg_additional_property(ir_graph *irg, mtp_additional_property flag) {
466 unsigned prop = irg->additional_properties;
468 if (prop & mtp_property_inherited)
469 prop = get_method_additional_properties(get_entity_type(irg->ent));
470 irg->additional_properties = prop | flag;
474 _set_irg_link(ir_graph *irg, void *thing) {
479 _get_irg_link(const ir_graph *irg) {
483 static INLINE unsigned long
484 _get_irg_visited(const ir_graph *irg) {
488 static INLINE unsigned long
489 _get_irg_block_visited(const ir_graph *irg) {
490 return irg->block_visited;
494 _set_irg_block_visited(ir_graph *irg, unsigned long visited) {
495 irg->block_visited = visited;
499 _inc_irg_block_visited(ir_graph *irg) {
500 ++irg->block_visited;
503 static INLINE unsigned
504 _get_irg_estimated_node_cnt(const ir_graph *irg) {
505 return irg->estimated_node_count;
508 #define get_interprocedural_view() _get_interprocedural_view()
509 #define is_ir_graph(thing) _is_ir_graph(thing)
510 #define get_irg_start_block(irg) _get_irg_start_block(irg)
511 #define set_irg_start_block(irg, node) _set_irg_start_block(irg, node)
512 #define get_irg_start(irg) _get_irg_start(irg)
513 #define set_irg_start(irg, node) _set_irg_start(irg, node)
514 #define get_irg_end_block(irg) _get_irg_end_block(irg)
515 #define set_irg_end_block(irg, node) _set_irg_end_block(irg, node)
516 #define get_irg_end(irg) _get_irg_end(irg)
517 #define set_irg_end(irg, node) _set_irg_end(irg, node)
518 #define get_irg_end_reg(irg) _get_irg_end_reg(irg)
519 #define get_irg_end_except(irg) _get_irg_end_except(irg)
520 #define get_irg_cstore(irg) _get_irg_cstore(irg)
521 #define set_irg_cstore(irg, node) _set_irg_cstore(irg, node)
522 #define get_irg_frame(irg) _get_irg_frame(irg)
523 #define set_irg_frame(irg, node) _set_irg_frame(irg, node)
524 #define get_irg_globals(irg) _get_irg_globals(irg)
525 #define set_irg_globals(irg, node) _set_irg_globals(irg, node)
526 #define get_irg_initial_mem(irg) _get_irg_initial_mem(irg)
527 #define set_irg_initial_mem(irg, node) _set_irg_initial_mem(irg, node)
528 #define get_irg_args(irg) _get_irg_args(irg)
529 #define set_irg_args(irg, node) _set_irg_args(irg, node)
530 #define get_irg_bad(irg) _get_irg_bad(irg)
531 #define set_irg_bad(irg, node) _set_irg_bad(irg, node)
532 #define get_irg_no_mem(irg) _get_irg_no_mem(irg)
533 #define set_irg_no_mem(irg, node) _set_irg_no_mem(irg, node)
534 #define get_irg_current_block(irg) _get_irg_current_block(irg)
535 #define set_irg_current_block(irg, node) _set_irg_current_block(irg, node)
536 #define get_irg_entity(irg) _get_irg_entity(irg)
537 #define set_irg_entity(irg, ent) _set_irg_entity(irg, ent)
538 #define get_irg_frame_type(irg) _get_irg_frame_type(irg)
539 #define set_irg_frame_type(irg, ftp) _set_irg_frame_type(irg, ftp)
540 #define get_irg_obstack(irg) _get_irg_obstack(irg)
541 #define get_irg_phase_state(irg) _get_irg_phase_state(irg)
542 #define set_irg_phase_low(irg) _set_irg_phase_low(irg)
543 #define get_irg_pinned(irg) _get_irg_pinned(irg)
544 #define get_irg_outs_state(irg) _get_irg_outs_state(irg)
545 #define set_irg_outs_inconsistent(irg) _set_irg_outs_inconsistent(irg)
546 #define get_irg_dom_state(irg) _get_irg_dom_state(irg)
547 #define set_irg_dom_inconsistent(irg) _set_irg_dom_inconsistent(irg)
548 #define get_irg_loopinfo_state(irg) _get_irg_loopinfo_state(irg)
549 #define set_irg_loopinfo_state(irg, s) _set_irg_loopinfo_state(irg, s)
550 #define set_irg_loopinfo_inconsistent(irg) _set_irg_loopinfo_inconsistent(irg)
551 #define set_irg_pinned(irg, p) _set_irg_pinned(irg, p)
552 #define get_irg_callee_info_state(irg) _get_irg_callee_info_state(irg)
553 #define set_irg_callee_info_state(irg, s) _set_irg_callee_info_state(irg, s)
554 #define get_irg_inline_property(irg) _get_irg_inline_property(irg)
555 #define set_irg_inline_property(irg, s) _set_irg_inline_property(irg, s)
556 #define get_irg_additional_properties(irg) _get_irg_additional_properties(irg)
557 #define set_irg_additional_properties(irg, m) _set_irg_additional_properties(irg, m)
558 #define set_irg_additional_property(irg, f) _set_irg_additional_property(irg, f)
559 #define set_irg_link(irg, thing) _set_irg_link(irg, thing)
560 #define get_irg_link(irg) _get_irg_link(irg)
561 #define get_irg_visited(irg) _get_irg_visited(irg)
562 #define get_irg_block_visited(irg) _get_irg_block_visited(irg)
563 #define set_irg_block_visited(irg, v) _set_irg_block_visited(irg, v)
564 #define inc_irg_block_visited(irg) _inc_irg_block_visited(irg)
565 #define get_irg_estimated_node_cnt(irg) _get_irg_estimated_node_cnt(irg)
567 # endif /* _IRGRAPH_T_H_ */