2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Entry point to the representation of procedure code -- internal header.
23 * @author Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
26 #ifndef FIRM_IR_IRGRAPH_T_H
27 #define FIRM_IR_IRGRAPH_T_H
29 #include "firm_types.h"
32 #include "firm_common_t.h"
33 #include "irtypeinfo.h"
35 #include "pseudo_irg.h"
39 #include "tr_inheritance.h"
40 #include "iredgekinds.h"
44 #include "execution_frequency.h"
50 /** Prefix that is added to every frame type. */
51 #define FRAME_TP_SUFFIX "frame_tp"
54 * Edge info to put into an irg.
56 typedef struct _irg_edge_info_t {
57 set *edges; /**< a set containing all edges of a graph. */
58 unsigned activated : 1; /**< set if edges are activated for the graph. */
61 typedef irg_edge_info_t irg_edges_info_t[EDGE_KIND_LAST];
64 * Index constants for nodes that can be accessed through the graph itself.
67 anchor_start_block = 0, /**< block the start node will belong to */
68 anchor_start, /**< start node of this ir_graph */
69 anchor_end_block, /**< block the end node will belong to */
70 anchor_end, /**< end node of this ir_graph */
71 anchor_end_reg, /**< end node of this ir_graph */
72 anchor_end_except, /**< end node of this ir_graph */
73 anchor_frame, /**< method's frame */
74 anchor_globals, /**< pointer to the data segment containing all
75 globals as well as global procedures. */
76 anchor_tls, /**< pointer to the thread local storage containing all
78 anchor_initial_mem, /**< initial memory of this graph */
79 anchor_args, /**< methods arguments */
80 anchor_value_param_base, /**< method value param base */
81 anchor_bad, /**< bad node of this ir_graph, the one and
83 anchor_no_mem, /**< NoMem node of this ir_graph, the one and only in this graph */
87 /** A callgraph entry for callees. */
88 typedef struct cg_callee_entry {
89 ir_graph *irg; /**< The called irg. */
90 ir_node **call_list; /**< The list of all calls to the irg. */
91 int max_depth; /**< Maximum depth of all Call nodes to irg. */
95 * An ir_graph holds all information for a procedure.
98 firm_kind kind; /**< Always set to k_ir_graph. */
99 /* -- Basics of the representation -- */
100 ir_entity *ent; /**< The entity of this procedure, i.e.,
101 the type of the procedure and the
102 class it belongs to. */
103 ir_type *frame_type; /**< A class type representing the stack frame.
104 Can include "inner" methods. */
105 ir_node *anchors[anchor_max]; /**< List of anchor nodes. */
106 ir_node **proj_args; /**< Projs of the methods arguments. */
107 struct obstack *obst; /**< The obstack where all of the ir_nodes live. */
108 ir_node *current_block; /**< Current block for newly gen_*()-erated ir_nodes. */
109 struct obstack *extbb_obst; /**< The obstack for extended basic block info. */
111 unsigned last_node_idx; /**< The last IR node index for this graph. */
113 /* -- Fields for graph properties -- */
114 irg_inline_property inline_property; /**< How to handle inlineing. */
115 unsigned additional_properties; /**< Additional graph properties. */
117 /* -- Fields indicating different states of irgraph -- */
118 irg_phase_state phase_state; /**< Compiler phase. */
119 op_pin_state irg_pinned_state; /**< Flag for status of nodes. */
120 irg_outs_state outs_state; /**< Out edges. */
121 irg_dom_state dom_state; /**< Dominator state information. */
122 irg_dom_state pdom_state; /**< Post Dominator state information. */
123 ir_typeinfo_state typeinfo_state; /**< Validity of type information. */
124 irg_callee_info_state callee_info_state; /**< Validity of callee information. */
125 irg_loopinfo_state loopinfo_state; /**< State of loop information. */
126 ir_class_cast_state class_cast_state; /**< Kind of cast operations in code. */
127 irg_extblk_info_state extblk_state; /**< State of extended basic block info. */
128 exec_freq_state execfreq_state; /**< Execution frequency state. */
129 ir_address_taken_computed_state adr_taken_state; /**< Address taken state. */
130 unsigned mem_disambig_opt; /**< Options for the memory disambiguator. */
131 unsigned fp_model; /**< floating point model of the graph. */
133 /* -- Fields for construction -- */
134 #if USE_EXPLICIT_PHI_IN_STACK
135 struct Phi_in_stack *Phi_in_stack; /**< Needed for automatic Phi construction. */
137 int n_loc; /**< Number of local variables in this
138 procedure including procedure parameters. */
139 void **loc_descriptions; /**< Storage for local variable descriptions. */
141 /* -- Fields for optimizations / analysis information -- */
142 pset *value_table; /**< Hash table for global value numbering (cse)
143 for optimizing use in iropt.c */
144 ir_node **outs; /**< Space for the out arrays. */
146 ir_loop *loop; /**< The outermost loop for this graph. */
147 void *link; /**< A void* field to link any information to
150 ir_graph **callers; /**< For callgraph analysis: list of caller graphs. */
151 unsigned char *caller_isbe; /**< For callgraph analysis: set if backedge. */
152 cg_callee_entry **callees; /**< For callgraph analysis: list of callee calls */
153 unsigned char *callee_isbe; /**< For callgraph analysis: set if backedge. */
154 int callgraph_loop_depth; /**< For callgraph analysis */
155 int callgraph_recursion_depth; /**< For callgraph analysis */
156 double method_execution_frequency; /**< For callgraph analysis */
158 ir_loop *l; /**< For callgraph analysis. */
160 /* -- Fields for Walking the graph -- */
161 unsigned long visited; /**< this flag is an identifier for
162 ir walk. it will be incremented
163 every time someone walks through
165 unsigned long block_visited; /**< same as visited, for a complete block */
168 unsigned using_visited : 1; /**< set to 1 if we are currently using the visited flag */
169 unsigned using_block_visited : 1; /**< set to 1 if we are currently using the block_visited flag */
170 unsigned using_irn_link : 1; /**< set to 1 if we are currently using the irn_link fields */
173 unsigned estimated_node_count; /**< estimated number of nodes in this graph,
174 updated after every walk */
175 irg_edges_info_t edge_info; /**< edge info for automatic outs */
176 ir_node **idx_irn_map; /**< Array mapping node indexes to nodes. */
179 int n_outs; /**< Size wasted for outs */
180 long graph_nr; /**< a unique graph number for each graph to make output
187 * Initializes the graph construction module
189 void firm_init_irgraph(void);
191 /* Internal constructor that does not add to irp_irgs or the like. */
192 ir_graph *new_r_ir_graph (ir_entity *ent, int n_loc);
194 /** Make a rudimentary ir graph for the constant code.
195 Must look like a correct irg, spare everything else. */
196 ir_graph *new_const_code_irg(void);
199 * Set the op_pin_state_pinned state of a graph.
201 * @param irg the IR graph
202 * @param p new pin state
205 set_irg_pinned (ir_graph *irg, op_pin_state p);
207 /** Returns the obstack associated with the graph. */
208 struct obstack *get_irg_obstack(const ir_graph *irg);
211 * Returns true if the node n is allocated on the storage of graph irg.
213 * @param irg the IR graph
214 * @param n the IR node
216 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n);
218 /*-------------------------------------------------------------------*/
219 /* inline functions for graphs */
220 /*-------------------------------------------------------------------*/
222 extern int firm_interprocedural_view;
225 _get_interprocedural_view(void) {
226 return firm_interprocedural_view;
230 _is_ir_graph(const void *thing) {
231 return (get_kind(thing) == k_ir_graph);
234 /** Returns the start block of a graph. */
235 static INLINE ir_node *
236 _get_irg_start_block(const ir_graph *irg) {
237 return irg->anchors[anchor_start_block];
241 _set_irg_start_block(ir_graph *irg, ir_node *node) {
242 irg->anchors[anchor_start_block] = node;
245 static INLINE ir_node *
246 _get_irg_start(const ir_graph *irg) {
247 return irg->anchors[anchor_start];
251 _set_irg_start(ir_graph *irg, ir_node *node) {
252 irg->anchors[anchor_start] = node;
255 static INLINE ir_node *
256 _get_irg_end_block(const ir_graph *irg) {
257 return irg->anchors[anchor_end_block];
261 _set_irg_end_block(ir_graph *irg, ir_node *node) {
262 irg->anchors[anchor_end_block] = node;
265 static INLINE ir_node *
266 _get_irg_end(const ir_graph *irg) {
267 return irg->anchors[anchor_end];
271 _set_irg_end(ir_graph *irg, ir_node *node) {
272 irg->anchors[anchor_end] = node;
275 static INLINE ir_node *
276 _get_irg_end_reg(const ir_graph *irg) {
277 return irg->anchors[anchor_end_reg];
280 static INLINE ir_node *
281 _get_irg_end_except (const ir_graph *irg) {
282 return irg->anchors[anchor_end_except];
285 static INLINE ir_node *
286 _get_irg_frame(const ir_graph *irg) {
287 return irg->anchors[anchor_frame];
291 _set_irg_frame(ir_graph *irg, ir_node *node) {
292 irg->anchors[anchor_frame] = node;
295 static INLINE ir_node *
296 _get_irg_globals(const ir_graph *irg) {
297 return irg->anchors[anchor_globals];
301 _set_irg_globals(ir_graph *irg, ir_node *node) {
302 irg->anchors[anchor_globals] = node;
305 static INLINE ir_node *
306 _get_irg_tls(const ir_graph *irg) {
307 return irg->anchors[anchor_tls];
311 _set_irg_tls(ir_graph *irg, ir_node *node) {
312 irg->anchors[anchor_tls] = node;
315 static INLINE ir_node *
316 _get_irg_initial_mem(const ir_graph *irg) {
317 return irg->anchors[anchor_initial_mem];
321 _set_irg_initial_mem(ir_graph *irg, ir_node *node) {
322 irg->anchors[anchor_initial_mem] = node;
325 static INLINE ir_node *
326 _get_irg_args(const ir_graph *irg) {
327 return irg->anchors[anchor_args];
331 _set_irg_args(ir_graph *irg, ir_node *node) {
332 irg->anchors[anchor_args] = node;
335 static INLINE ir_node *
336 _get_irg_value_param_base(const ir_graph *irg) {
337 return irg->anchors[anchor_value_param_base];
341 _set_irg_value_param_base(ir_graph *irg, ir_node *node) {
342 irg->anchors[anchor_value_param_base] = node;
345 static INLINE ir_node **
346 _get_irg_proj_args(const ir_graph *irg) {
347 return irg->proj_args;
351 _set_irg_proj_args(ir_graph *irg, ir_node **nodes) {
352 irg->proj_args = nodes;
355 static INLINE ir_node *
356 _get_irg_bad(const ir_graph *irg) {
357 return irg->anchors[anchor_bad];
361 _set_irg_bad(ir_graph *irg, ir_node *node) {
362 irg->anchors[anchor_bad] = node;
365 static INLINE ir_node *
366 _get_irg_no_mem(const ir_graph *irg) {
367 return irg->anchors[anchor_no_mem];
371 _set_irg_no_mem(ir_graph *irg, ir_node *node) {
372 irg->anchors[anchor_no_mem] = node;
374 static INLINE ir_node *
375 _get_irg_current_block(const ir_graph *irg) {
376 return irg->current_block;
380 _set_irg_current_block(ir_graph *irg, ir_node *node) {
381 irg->current_block = node;
384 static INLINE ir_entity *
385 _get_irg_entity(const ir_graph *irg) {
386 assert(irg && irg->ent);
391 _set_irg_entity(ir_graph *irg, ir_entity *ent) {
395 static INLINE ir_type *
396 _get_irg_frame_type(ir_graph *irg) {
397 assert(irg && irg->frame_type);
398 return irg->frame_type = skip_tid(irg->frame_type);
402 _set_irg_frame_type(ir_graph *irg, ir_type *ftp) {
403 assert(is_frame_type(ftp));
404 irg->frame_type = ftp;
407 static INLINE struct obstack *
408 _get_irg_obstack(const ir_graph *irg) {
413 static INLINE irg_phase_state
414 _get_irg_phase_state(const ir_graph *irg) {
415 return irg->phase_state;
419 _set_irg_phase_state(ir_graph *irg, irg_phase_state state) {
420 irg->phase_state = state;
423 static INLINE op_pin_state
424 _get_irg_pinned(const ir_graph *irg) {
425 return irg->irg_pinned_state;
428 static INLINE irg_outs_state
429 _get_irg_outs_state(const ir_graph *irg) {
430 return irg->outs_state;
434 _set_irg_outs_inconsistent(ir_graph *irg) {
435 if (irg->outs_state == outs_consistent)
436 irg->outs_state = outs_inconsistent;
439 static INLINE irg_extblk_state
440 _get_irg_extblk_state(const ir_graph *irg) {
441 return irg->extblk_state;
445 _set_irg_extblk_inconsistent(ir_graph *irg) {
446 if (irg->extblk_state == extblk_valid)
447 irg->extblk_state = extblk_invalid;
450 static INLINE irg_dom_state
451 _get_irg_dom_state(const ir_graph *irg) {
452 return irg->dom_state;
455 static INLINE irg_dom_state
456 _get_irg_postdom_state(const ir_graph *irg) {
457 return irg->pdom_state;
461 _set_irg_doms_inconsistent(ir_graph *irg) {
462 if (irg->dom_state != dom_none)
463 irg->dom_state = dom_inconsistent;
464 if (irg->pdom_state != dom_none)
465 irg->pdom_state = dom_inconsistent;
468 static INLINE irg_loopinfo_state
469 _get_irg_loopinfo_state(const ir_graph *irg) {
470 return irg->loopinfo_state;
474 _set_irg_loopinfo_state(ir_graph *irg, irg_loopinfo_state s) {
475 irg->loopinfo_state = s;
479 _set_irg_loopinfo_inconsistent(ir_graph *irg) {
480 irg->loopinfo_state &= ~loopinfo_valid;
484 _set_irg_pinned(ir_graph *irg, op_pin_state p) {
485 irg->irg_pinned_state = p;
488 static INLINE irg_callee_info_state
489 _get_irg_callee_info_state(const ir_graph *irg) {
490 return irg->callee_info_state;
494 _set_irg_callee_info_state(ir_graph *irg, irg_callee_info_state s) {
495 irg_callee_info_state irp_state = get_irp_callee_info_state();
497 irg->callee_info_state = s;
499 /* I could compare ... but who knows? */
500 if ((irp_state == irg_callee_info_consistent) ||
501 ((irp_state == irg_callee_info_inconsistent) && (s == irg_callee_info_none)))
502 set_irp_callee_info_state(s);
505 static INLINE irg_inline_property
506 _get_irg_inline_property(const ir_graph *irg) {
507 return irg->inline_property;
511 _set_irg_inline_property(ir_graph *irg, irg_inline_property s) {
512 irg->inline_property = s;
515 static INLINE unsigned
516 _get_irg_additional_properties(const ir_graph *irg) {
517 if (irg->additional_properties & mtp_property_inherited)
518 return get_method_additional_properties(get_entity_type(irg->ent));
519 return irg->additional_properties;
523 _set_irg_additional_properties(ir_graph *irg, unsigned mask) {
524 /* do not allow to set the mtp_property_inherited flag or
525 * the automatic inheritance of flags will not work */
526 irg->additional_properties = mask & ~mtp_property_inherited;
530 _set_irg_additional_property(ir_graph *irg, mtp_additional_property flag) {
531 unsigned prop = irg->additional_properties;
533 if (prop & mtp_property_inherited)
534 prop = get_method_additional_properties(get_entity_type(irg->ent));
535 irg->additional_properties = prop | flag;
539 _set_irg_link(ir_graph *irg, void *thing) {
544 _get_irg_link(const ir_graph *irg) {
548 static INLINE unsigned long
549 _get_irg_visited(const ir_graph *irg) {
553 static INLINE unsigned long
554 _get_irg_block_visited(const ir_graph *irg) {
555 return irg->block_visited;
559 _set_irg_block_visited(ir_graph *irg, unsigned long visited) {
560 irg->block_visited = visited;
564 _inc_irg_block_visited(ir_graph *irg) {
565 ++irg->block_visited;
569 _dec_irg_block_visited(ir_graph *irg) {
570 --irg->block_visited;
573 static INLINE unsigned
574 _get_irg_estimated_node_cnt(const ir_graph *irg) {
575 return irg->estimated_node_count;
578 /* Return the floating point model of this graph. */
579 static INLINE unsigned
580 _get_irg_fp_model(const ir_graph *irg) {
581 return irg->fp_model;
585 * Allocates a new idx in the irg for the node and adds the irn to the idx -> irn map.
586 * @param irg The graph.
587 * @param irn The node.
588 * @return The index allocated for the node.
590 static INLINE unsigned
591 irg_register_node_idx(ir_graph *irg, ir_node *irn)
593 unsigned idx = irg->last_node_idx++;
594 if(idx >= (unsigned) ARR_LEN(irg->idx_irn_map))
595 ARR_RESIZE(ir_node *, irg->idx_irn_map, idx + 1);
597 irg->idx_irn_map[idx] = irn;
602 * Kill a node from the irg. BEWARE: this kills
603 * all later created nodes.
606 irg_kill_node(ir_graph *irg, ir_node *n) {
607 unsigned idx = get_irn_idx(n);
608 if (idx + 1 == irg->last_node_idx)
609 --irg->last_node_idx;
610 irg->idx_irn_map[idx] = NULL;
611 obstack_free(irg->obst, n);
615 * Get the node for an index.
616 * @param irg The graph.
617 * @param idx The index you want the node for.
618 * @return The node with that index or NULL, if there is no node with that index.
619 * @note The node you got might be dead.
621 static INLINE ir_node *
622 get_idx_irn(ir_graph *irg, unsigned idx) {
623 assert(idx < (unsigned) ARR_LEN(irg->idx_irn_map));
624 return irg->idx_irn_map[idx];
627 #define get_interprocedural_view() _get_interprocedural_view()
628 #define is_ir_graph(thing) _is_ir_graph(thing)
629 #define get_irg_start_block(irg) _get_irg_start_block(irg)
630 #define set_irg_start_block(irg, node) _set_irg_start_block(irg, node)
631 #define get_irg_start(irg) _get_irg_start(irg)
632 #define set_irg_start(irg, node) _set_irg_start(irg, node)
633 #define get_irg_end_block(irg) _get_irg_end_block(irg)
634 #define set_irg_end_block(irg, node) _set_irg_end_block(irg, node)
635 #define get_irg_end(irg) _get_irg_end(irg)
636 #define set_irg_end(irg, node) _set_irg_end(irg, node)
637 #define get_irg_end_reg(irg) _get_irg_end_reg(irg)
638 #define get_irg_end_except(irg) _get_irg_end_except(irg)
639 #define get_irg_frame(irg) _get_irg_frame(irg)
640 #define set_irg_frame(irg, node) _set_irg_frame(irg, node)
641 #define get_irg_globals(irg) _get_irg_globals(irg)
642 #define set_irg_globals(irg, node) _set_irg_globals(irg, node)
643 #define get_irg_tls(irg) _get_irg_tls(irg)
644 #define set_irg_tls(irg, node) _set_irg_tls(irg, node)
645 #define get_irg_initial_mem(irg) _get_irg_initial_mem(irg)
646 #define set_irg_initial_mem(irg, node) _set_irg_initial_mem(irg, node)
647 #define get_irg_args(irg) _get_irg_args(irg)
648 #define set_irg_args(irg, node) _set_irg_args(irg, node)
649 #define get_irg_value_param_base(irg) _get_irg_value_param_base(irg)
650 #define set_irg_value_param_base(irg, node) _set_irg_value_param_base(irg, node)
651 #define get_irg_bad(irg) _get_irg_bad(irg)
652 #define set_irg_bad(irg, node) _set_irg_bad(irg, node)
653 #define get_irg_no_mem(irg) _get_irg_no_mem(irg)
654 #define set_irg_no_mem(irg, node) _set_irg_no_mem(irg, node)
655 #define get_irg_current_block(irg) _get_irg_current_block(irg)
656 #define set_irg_current_block(irg, node) _set_irg_current_block(irg, node)
657 #define get_irg_entity(irg) _get_irg_entity(irg)
658 #define set_irg_entity(irg, ent) _set_irg_entity(irg, ent)
659 #define get_irg_frame_type(irg) _get_irg_frame_type(irg)
660 #define set_irg_frame_type(irg, ftp) _set_irg_frame_type(irg, ftp)
661 #define get_irg_obstack(irg) _get_irg_obstack(irg)
662 #define get_irg_phase_state(irg) _get_irg_phase_state(irg)
663 #define set_irg_phase_state(irg, state) _set_irg_phase_state(irg, state)
664 #define get_irg_pinned(irg) _get_irg_pinned(irg)
665 #define get_irg_outs_state(irg) _get_irg_outs_state(irg)
666 #define set_irg_outs_inconsistent(irg) _set_irg_outs_inconsistent(irg)
667 #define get_irg_extblk_state(irg) _get_irg_extblk_state(irg)
668 #define set_irg_extblk_inconsistent(irg) _set_irg_extblk_inconsistent(irg)
669 #define get_irg_dom_state(irg) _get_irg_dom_state(irg)
670 #define get_irg_postdom_state(irg) _get_irg_postdom_state(irg)
671 #define set_irg_doms_inconsistent(irg) _set_irg_doms_inconsistent(irg)
672 #define get_irg_loopinfo_state(irg) _get_irg_loopinfo_state(irg)
673 #define set_irg_loopinfo_state(irg, s) _set_irg_loopinfo_state(irg, s)
674 #define set_irg_loopinfo_inconsistent(irg) _set_irg_loopinfo_inconsistent(irg)
675 #define set_irg_pinned(irg, p) _set_irg_pinned(irg, p)
676 #define get_irg_callee_info_state(irg) _get_irg_callee_info_state(irg)
677 #define set_irg_callee_info_state(irg, s) _set_irg_callee_info_state(irg, s)
678 #define get_irg_inline_property(irg) _get_irg_inline_property(irg)
679 #define set_irg_inline_property(irg, s) _set_irg_inline_property(irg, s)
680 #define get_irg_additional_properties(irg) _get_irg_additional_properties(irg)
681 #define set_irg_additional_properties(irg, m) _set_irg_additional_properties(irg, m)
682 #define set_irg_additional_property(irg, f) _set_irg_additional_property(irg, f)
683 #define set_irg_link(irg, thing) _set_irg_link(irg, thing)
684 #define get_irg_link(irg) _get_irg_link(irg)
685 #define get_irg_visited(irg) _get_irg_visited(irg)
686 #define get_irg_block_visited(irg) _get_irg_block_visited(irg)
687 #define set_irg_block_visited(irg, v) _set_irg_block_visited(irg, v)
688 #define inc_irg_block_visited(irg) _inc_irg_block_visited(irg)
689 #define dec_irg_block_visited(irg) _dec_irg_block_visited(irg)
690 #define get_irg_estimated_node_cnt(irg) _get_irg_estimated_node_cnt(irg)
691 #define get_irg_fp_model(irg) _get_irg_fp_model(irg)