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"
38 #include "iredgekinds.h"
42 #include "execution_frequency.h"
48 /** Prefix that is added to every frame type. */
49 #define FRAME_TP_SUFFIX "frame_tp"
52 * Edge info to put into an irg.
54 typedef struct _irg_edge_info_t {
55 set *edges; /**< a set containing all edges of a graph. */
56 unsigned activated : 1; /**< set if edges are activated for the graph. */
59 typedef irg_edge_info_t irg_edges_info_t[EDGE_KIND_LAST];
62 * Index constants for nodes that can be accessed through the graph itself.
65 anchor_start_block = 0, /**< block the start node will belong to */
66 anchor_start, /**< start node of this ir_graph */
67 anchor_end_block, /**< block the end node will belong to */
68 anchor_end, /**< end node of this ir_graph */
69 anchor_end_reg, /**< end node of this ir_graph */
70 anchor_end_except, /**< end node of this ir_graph */
71 anchor_frame, /**< method's frame */
72 anchor_globals, /**< pointer to the data segment containing all
73 globals as well as global procedures. */
74 anchor_tls, /**< pointer to the thread local storage containing all
76 anchor_initial_mem, /**< initial memory of this graph */
77 anchor_args, /**< methods arguments */
78 anchor_value_param_base, /**< method value param base */
79 anchor_bad, /**< bad node of this ir_graph, the one and
81 anchor_no_mem, /**< NoMem node of this ir_graph, the one and only in this graph */
85 /** A callgraph entry for callees. */
86 typedef struct cg_callee_entry {
87 ir_graph *irg; /**< The called irg. */
88 ir_node **call_list; /**< The list of all calls to the irg. */
89 int max_depth; /**< Maximum depth of all Call nodes to irg. */
93 * An ir_graph holds all information for a procedure.
96 firm_kind kind; /**< Always set to k_ir_graph. */
97 /* -- Basics of the representation -- */
98 ir_entity *ent; /**< The entity of this procedure, i.e.,
99 the type of the procedure and the
100 class it belongs to. */
101 ir_type *frame_type; /**< A class type representing the stack frame.
102 Can include "inner" methods. */
103 ir_node *anchors[anchor_max]; /**< List of anchor nodes. */
104 ir_node **proj_args; /**< Projs of the methods arguments. */
105 struct obstack *obst; /**< The obstack where all of the ir_nodes live. */
106 ir_node *current_block; /**< Current block for newly gen_*()-erated ir_nodes. */
107 struct obstack *extbb_obst; /**< The obstack for extended basic block info. */
109 unsigned last_node_idx; /**< The last IR node index for this graph. */
111 /* -- Fields for graph properties -- */
112 irg_inline_property inline_property; /**< How to handle inlineing. */
113 unsigned additional_properties; /**< Additional graph properties. */
115 /* -- Fields indicating different states of irgraph -- */
116 irg_phase_state phase_state; /**< Compiler phase. */
117 op_pin_state irg_pinned_state; /**< Flag for status of nodes. */
118 irg_outs_state outs_state; /**< Out edges. */
119 irg_dom_state dom_state; /**< Dominator state information. */
120 irg_dom_state pdom_state; /**< Post Dominator state information. */
121 ir_typeinfo_state typeinfo_state; /**< Validity of type information. */
122 irg_callee_info_state callee_info_state; /**< Validity of callee information. */
123 irg_loopinfo_state loopinfo_state; /**< State of loop information. */
124 ir_class_cast_state class_cast_state; /**< Kind of cast operations in code. */
125 irg_extblk_info_state extblk_state; /**< State of extended basic block info. */
126 exec_freq_state execfreq_state; /**< Execution frequency state. */
127 ir_address_taken_computed_state adr_taken_state; /**< Address taken state. */
128 unsigned mem_disambig_opt; /**< Options for the memory disambiguator. */
129 unsigned fp_model; /**< floating point model of the graph. */
131 /* -- Fields for construction -- */
132 #if USE_EXPLICIT_PHI_IN_STACK
133 struct Phi_in_stack *Phi_in_stack; /**< Needed for automatic Phi construction. */
135 int n_loc; /**< Number of local variables in this
136 procedure including procedure parameters. */
137 void **loc_descriptions; /**< Storage for local variable descriptions. */
139 /* -- Fields for optimizations / analysis information -- */
140 pset *value_table; /**< Hash table for global value numbering (cse)
141 for optimizing use in iropt.c */
142 ir_node **outs; /**< Space for the out arrays. */
144 ir_loop *loop; /**< The outermost loop for this graph. */
145 void *link; /**< A void* field to link any information to
148 ir_graph **callers; /**< For callgraph analysis: list of caller graphs. */
149 unsigned char *caller_isbe; /**< For callgraph analysis: set if backedge. */
150 cg_callee_entry **callees; /**< For callgraph analysis: list of callee calls */
151 unsigned char *callee_isbe; /**< For callgraph analysis: set if backedge. */
152 int callgraph_loop_depth; /**< For callgraph analysis */
153 int callgraph_recursion_depth; /**< For callgraph analysis */
154 double method_execution_frequency; /**< For callgraph analysis */
156 ir_loop *l; /**< For callgraph analysis. */
158 /* -- Fields for Walking the graph -- */
159 unsigned long visited; /**< this flag is an identifier for
160 ir walk. it will be incremented
161 every time someone walks through
163 unsigned long block_visited; /**< same as visited, for a complete block */
166 unsigned using_visited : 1; /**< set to 1 if we are currently using the visited flag */
167 unsigned using_block_visited : 1; /**< set to 1 if we are currently using the block_visited flag */
168 unsigned using_irn_link : 1; /**< set to 1 if we are currently using the irn_link fields */
171 unsigned estimated_node_count; /**< estimated number of nodes in this graph,
172 updated after every walk */
173 irg_edges_info_t edge_info; /**< edge info for automatic outs */
174 ir_node **idx_irn_map; /**< Array mapping node indexes to nodes. */
177 int n_outs; /**< Size wasted for outs */
178 long graph_nr; /**< a unique graph number for each graph to make output
185 * Initializes the graph construction module
187 void firm_init_irgraph(void);
189 /* Internal constructor that does not add to irp_irgs or the like. */
190 ir_graph *new_r_ir_graph (ir_entity *ent, int n_loc);
192 /** Make a rudimentary ir graph for the constant code.
193 Must look like a correct irg, spare everything else. */
194 ir_graph *new_const_code_irg(void);
197 * Set the op_pin_state_pinned state of a graph.
199 * @param irg the IR graph
200 * @param p new pin state
203 set_irg_pinned (ir_graph *irg, op_pin_state p);
205 /** Returns the obstack associated with the graph. */
206 struct obstack *get_irg_obstack(const ir_graph *irg);
209 * Returns true if the node n is allocated on the storage of graph irg.
211 * @param irg the IR graph
212 * @param n the IR node
214 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n);
216 /*-------------------------------------------------------------------*/
217 /* inline functions for graphs */
218 /*-------------------------------------------------------------------*/
220 extern int firm_interprocedural_view;
223 _get_interprocedural_view(void) {
224 return firm_interprocedural_view;
228 _is_ir_graph(const void *thing) {
229 return (get_kind(thing) == k_ir_graph);
232 /** Returns the start block of a graph. */
233 static INLINE ir_node *
234 _get_irg_start_block(const ir_graph *irg) {
235 return irg->anchors[anchor_start_block];
239 _set_irg_start_block(ir_graph *irg, ir_node *node) {
240 irg->anchors[anchor_start_block] = node;
243 static INLINE ir_node *
244 _get_irg_start(const ir_graph *irg) {
245 return irg->anchors[anchor_start];
249 _set_irg_start(ir_graph *irg, ir_node *node) {
250 irg->anchors[anchor_start] = node;
253 static INLINE ir_node *
254 _get_irg_end_block(const ir_graph *irg) {
255 return irg->anchors[anchor_end_block];
259 _set_irg_end_block(ir_graph *irg, ir_node *node) {
260 irg->anchors[anchor_end_block] = node;
263 static INLINE ir_node *
264 _get_irg_end(const ir_graph *irg) {
265 return irg->anchors[anchor_end];
269 _set_irg_end(ir_graph *irg, ir_node *node) {
270 irg->anchors[anchor_end] = node;
273 static INLINE ir_node *
274 _get_irg_end_reg(const ir_graph *irg) {
275 return irg->anchors[anchor_end_reg];
278 static INLINE ir_node *
279 _get_irg_end_except (const ir_graph *irg) {
280 return irg->anchors[anchor_end_except];
283 static INLINE ir_node *
284 _get_irg_frame(const ir_graph *irg) {
285 return irg->anchors[anchor_frame];
289 _set_irg_frame(ir_graph *irg, ir_node *node) {
290 irg->anchors[anchor_frame] = node;
293 static INLINE ir_node *
294 _get_irg_globals(const ir_graph *irg) {
295 return irg->anchors[anchor_globals];
299 _set_irg_globals(ir_graph *irg, ir_node *node) {
300 irg->anchors[anchor_globals] = node;
303 static INLINE ir_node *
304 _get_irg_tls(const ir_graph *irg) {
305 return irg->anchors[anchor_tls];
309 _set_irg_tls(ir_graph *irg, ir_node *node) {
310 irg->anchors[anchor_tls] = node;
313 static INLINE ir_node *
314 _get_irg_initial_mem(const ir_graph *irg) {
315 return irg->anchors[anchor_initial_mem];
319 _set_irg_initial_mem(ir_graph *irg, ir_node *node) {
320 irg->anchors[anchor_initial_mem] = node;
323 static INLINE ir_node *
324 _get_irg_args(const ir_graph *irg) {
325 return irg->anchors[anchor_args];
329 _set_irg_args(ir_graph *irg, ir_node *node) {
330 irg->anchors[anchor_args] = node;
333 static INLINE ir_node *
334 _get_irg_value_param_base(const ir_graph *irg) {
335 return irg->anchors[anchor_value_param_base];
339 _set_irg_value_param_base(ir_graph *irg, ir_node *node) {
340 irg->anchors[anchor_value_param_base] = node;
343 static INLINE ir_node **
344 _get_irg_proj_args(const ir_graph *irg) {
345 return irg->proj_args;
349 _set_irg_proj_args(ir_graph *irg, ir_node **nodes) {
350 irg->proj_args = nodes;
353 static INLINE ir_node *
354 _get_irg_bad(const ir_graph *irg) {
355 return irg->anchors[anchor_bad];
359 _set_irg_bad(ir_graph *irg, ir_node *node) {
360 irg->anchors[anchor_bad] = node;
363 static INLINE ir_node *
364 _get_irg_no_mem(const ir_graph *irg) {
365 return irg->anchors[anchor_no_mem];
369 _set_irg_no_mem(ir_graph *irg, ir_node *node) {
370 irg->anchors[anchor_no_mem] = node;
372 static INLINE ir_node *
373 _get_irg_current_block(const ir_graph *irg) {
374 return irg->current_block;
378 _set_irg_current_block(ir_graph *irg, ir_node *node) {
379 irg->current_block = node;
382 static INLINE ir_entity *
383 _get_irg_entity(const ir_graph *irg) {
384 assert(irg && irg->ent);
389 _set_irg_entity(ir_graph *irg, ir_entity *ent) {
393 static INLINE ir_type *
394 _get_irg_frame_type(ir_graph *irg) {
395 assert(irg && irg->frame_type);
396 return irg->frame_type = skip_tid(irg->frame_type);
400 _set_irg_frame_type(ir_graph *irg, ir_type *ftp) {
401 assert(is_frame_type(ftp));
402 irg->frame_type = ftp;
405 static INLINE struct obstack *
406 _get_irg_obstack(const ir_graph *irg) {
411 static INLINE irg_phase_state
412 _get_irg_phase_state(const ir_graph *irg) {
413 return irg->phase_state;
417 _set_irg_phase_state(ir_graph *irg, irg_phase_state state) {
418 irg->phase_state = state;
421 static INLINE op_pin_state
422 _get_irg_pinned(const ir_graph *irg) {
423 return irg->irg_pinned_state;
426 static INLINE irg_outs_state
427 _get_irg_outs_state(const ir_graph *irg) {
428 return irg->outs_state;
432 _set_irg_outs_inconsistent(ir_graph *irg) {
433 if (irg->outs_state == outs_consistent)
434 irg->outs_state = outs_inconsistent;
437 static INLINE irg_extblk_state
438 _get_irg_extblk_state(const ir_graph *irg) {
439 return irg->extblk_state;
443 _set_irg_extblk_inconsistent(ir_graph *irg) {
444 if (irg->extblk_state == extblk_valid)
445 irg->extblk_state = extblk_invalid;
448 static INLINE irg_dom_state
449 _get_irg_dom_state(const ir_graph *irg) {
450 return irg->dom_state;
453 static INLINE irg_dom_state
454 _get_irg_postdom_state(const ir_graph *irg) {
455 return irg->pdom_state;
459 _set_irg_doms_inconsistent(ir_graph *irg) {
460 if (irg->dom_state != dom_none)
461 irg->dom_state = dom_inconsistent;
462 if (irg->pdom_state != dom_none)
463 irg->pdom_state = dom_inconsistent;
466 static INLINE irg_loopinfo_state
467 _get_irg_loopinfo_state(const ir_graph *irg) {
468 return irg->loopinfo_state;
472 _set_irg_loopinfo_state(ir_graph *irg, irg_loopinfo_state s) {
473 irg->loopinfo_state = s;
477 _set_irg_loopinfo_inconsistent(ir_graph *irg) {
478 irg->loopinfo_state &= ~loopinfo_valid;
482 _set_irg_pinned(ir_graph *irg, op_pin_state p) {
483 irg->irg_pinned_state = p;
486 static INLINE irg_callee_info_state
487 _get_irg_callee_info_state(const ir_graph *irg) {
488 return irg->callee_info_state;
492 _set_irg_callee_info_state(ir_graph *irg, irg_callee_info_state s) {
493 irg_callee_info_state irp_state = get_irp_callee_info_state();
495 irg->callee_info_state = s;
497 /* I could compare ... but who knows? */
498 if ((irp_state == irg_callee_info_consistent) ||
499 ((irp_state == irg_callee_info_inconsistent) && (s == irg_callee_info_none)))
500 set_irp_callee_info_state(s);
503 static INLINE irg_inline_property
504 _get_irg_inline_property(const ir_graph *irg) {
505 return irg->inline_property;
509 _set_irg_inline_property(ir_graph *irg, irg_inline_property s) {
510 irg->inline_property = s;
513 static INLINE unsigned
514 _get_irg_additional_properties(const ir_graph *irg) {
515 if (irg->additional_properties & mtp_property_inherited)
516 return get_method_additional_properties(get_entity_type(irg->ent));
517 return irg->additional_properties;
521 _set_irg_additional_properties(ir_graph *irg, unsigned mask) {
522 /* do not allow to set the mtp_property_inherited flag or
523 * the automatic inheritance of flags will not work */
524 irg->additional_properties = mask & ~mtp_property_inherited;
528 _set_irg_additional_property(ir_graph *irg, mtp_additional_property flag) {
529 unsigned prop = irg->additional_properties;
531 if (prop & mtp_property_inherited)
532 prop = get_method_additional_properties(get_entity_type(irg->ent));
533 irg->additional_properties = prop | flag;
537 _set_irg_link(ir_graph *irg, void *thing) {
542 _get_irg_link(const ir_graph *irg) {
546 static INLINE unsigned long
547 _get_irg_visited(const ir_graph *irg) {
551 static INLINE unsigned long
552 _get_irg_block_visited(const ir_graph *irg) {
553 return irg->block_visited;
557 _set_irg_block_visited(ir_graph *irg, unsigned long visited) {
558 irg->block_visited = visited;
562 _inc_irg_block_visited(ir_graph *irg) {
563 ++irg->block_visited;
567 _dec_irg_block_visited(ir_graph *irg) {
568 --irg->block_visited;
571 static INLINE unsigned
572 _get_irg_estimated_node_cnt(const ir_graph *irg) {
573 return irg->estimated_node_count;
576 /* Return the floating point model of this graph. */
577 static INLINE unsigned
578 _get_irg_fp_model(const ir_graph *irg) {
579 return irg->fp_model;
583 * Allocates a new idx in the irg for the node and adds the irn to the idx -> irn map.
584 * @param irg The graph.
585 * @param irn The node.
586 * @return The index allocated for the node.
588 static INLINE unsigned
589 irg_register_node_idx(ir_graph *irg, ir_node *irn)
591 unsigned idx = irg->last_node_idx++;
592 if(idx >= (unsigned) ARR_LEN(irg->idx_irn_map))
593 ARR_RESIZE(ir_node *, irg->idx_irn_map, idx + 1);
595 irg->idx_irn_map[idx] = irn;
600 * Kill a node from the irg. BEWARE: this kills
601 * all later created nodes.
604 irg_kill_node(ir_graph *irg, ir_node *n) {
605 unsigned idx = get_irn_idx(n);
606 if (idx + 1 == irg->last_node_idx)
607 --irg->last_node_idx;
608 irg->idx_irn_map[idx] = NULL;
609 obstack_free(irg->obst, n);
613 * Get the node for an index.
614 * @param irg The graph.
615 * @param idx The index you want the node for.
616 * @return The node with that index or NULL, if there is no node with that index.
617 * @note The node you got might be dead.
619 static INLINE ir_node *
620 get_idx_irn(ir_graph *irg, unsigned idx) {
621 assert(idx < (unsigned) ARR_LEN(irg->idx_irn_map));
622 return irg->idx_irn_map[idx];
625 #define get_interprocedural_view() _get_interprocedural_view()
626 #define is_ir_graph(thing) _is_ir_graph(thing)
627 #define get_irg_start_block(irg) _get_irg_start_block(irg)
628 #define set_irg_start_block(irg, node) _set_irg_start_block(irg, node)
629 #define get_irg_start(irg) _get_irg_start(irg)
630 #define set_irg_start(irg, node) _set_irg_start(irg, node)
631 #define get_irg_end_block(irg) _get_irg_end_block(irg)
632 #define set_irg_end_block(irg, node) _set_irg_end_block(irg, node)
633 #define get_irg_end(irg) _get_irg_end(irg)
634 #define set_irg_end(irg, node) _set_irg_end(irg, node)
635 #define get_irg_end_reg(irg) _get_irg_end_reg(irg)
636 #define get_irg_end_except(irg) _get_irg_end_except(irg)
637 #define get_irg_frame(irg) _get_irg_frame(irg)
638 #define set_irg_frame(irg, node) _set_irg_frame(irg, node)
639 #define get_irg_globals(irg) _get_irg_globals(irg)
640 #define set_irg_globals(irg, node) _set_irg_globals(irg, node)
641 #define get_irg_tls(irg) _get_irg_tls(irg)
642 #define set_irg_tls(irg, node) _set_irg_tls(irg, node)
643 #define get_irg_initial_mem(irg) _get_irg_initial_mem(irg)
644 #define set_irg_initial_mem(irg, node) _set_irg_initial_mem(irg, node)
645 #define get_irg_args(irg) _get_irg_args(irg)
646 #define set_irg_args(irg, node) _set_irg_args(irg, node)
647 #define get_irg_value_param_base(irg) _get_irg_value_param_base(irg)
648 #define set_irg_value_param_base(irg, node) _set_irg_value_param_base(irg, node)
649 #define get_irg_bad(irg) _get_irg_bad(irg)
650 #define set_irg_bad(irg, node) _set_irg_bad(irg, node)
651 #define get_irg_no_mem(irg) _get_irg_no_mem(irg)
652 #define set_irg_no_mem(irg, node) _set_irg_no_mem(irg, node)
653 #define get_irg_current_block(irg) _get_irg_current_block(irg)
654 #define set_irg_current_block(irg, node) _set_irg_current_block(irg, node)
655 #define get_irg_entity(irg) _get_irg_entity(irg)
656 #define set_irg_entity(irg, ent) _set_irg_entity(irg, ent)
657 #define get_irg_frame_type(irg) _get_irg_frame_type(irg)
658 #define set_irg_frame_type(irg, ftp) _set_irg_frame_type(irg, ftp)
659 #define get_irg_obstack(irg) _get_irg_obstack(irg)
660 #define get_irg_phase_state(irg) _get_irg_phase_state(irg)
661 #define set_irg_phase_state(irg, state) _set_irg_phase_state(irg, state)
662 #define get_irg_pinned(irg) _get_irg_pinned(irg)
663 #define get_irg_outs_state(irg) _get_irg_outs_state(irg)
664 #define set_irg_outs_inconsistent(irg) _set_irg_outs_inconsistent(irg)
665 #define get_irg_extblk_state(irg) _get_irg_extblk_state(irg)
666 #define set_irg_extblk_inconsistent(irg) _set_irg_extblk_inconsistent(irg)
667 #define get_irg_dom_state(irg) _get_irg_dom_state(irg)
668 #define get_irg_postdom_state(irg) _get_irg_postdom_state(irg)
669 #define set_irg_doms_inconsistent(irg) _set_irg_doms_inconsistent(irg)
670 #define get_irg_loopinfo_state(irg) _get_irg_loopinfo_state(irg)
671 #define set_irg_loopinfo_state(irg, s) _set_irg_loopinfo_state(irg, s)
672 #define set_irg_loopinfo_inconsistent(irg) _set_irg_loopinfo_inconsistent(irg)
673 #define set_irg_pinned(irg, p) _set_irg_pinned(irg, p)
674 #define get_irg_callee_info_state(irg) _get_irg_callee_info_state(irg)
675 #define set_irg_callee_info_state(irg, s) _set_irg_callee_info_state(irg, s)
676 #define get_irg_inline_property(irg) _get_irg_inline_property(irg)
677 #define set_irg_inline_property(irg, s) _set_irg_inline_property(irg, s)
678 #define get_irg_additional_properties(irg) _get_irg_additional_properties(irg)
679 #define set_irg_additional_properties(irg, m) _set_irg_additional_properties(irg, m)
680 #define set_irg_additional_property(irg, f) _set_irg_additional_property(irg, f)
681 #define set_irg_link(irg, thing) _set_irg_link(irg, thing)
682 #define get_irg_link(irg) _get_irg_link(irg)
683 #define get_irg_visited(irg) _get_irg_visited(irg)
684 #define get_irg_block_visited(irg) _get_irg_block_visited(irg)
685 #define set_irg_block_visited(irg, v) _set_irg_block_visited(irg, v)
686 #define inc_irg_block_visited(irg) _inc_irg_block_visited(irg)
687 #define dec_irg_block_visited(irg) _dec_irg_block_visited(irg)
688 #define get_irg_estimated_node_cnt(irg) _get_irg_estimated_node_cnt(irg)
689 #define get_irg_fp_model(irg) _get_irg_fp_model(irg)